110,538
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 更新章节内容
/// </summary>
/// <param name="book"></param>
private async void DownloadText(BookModel book)
{
//这个是无缓存查询
var directories = directory.GetList<Model.DirectoryModel>(d => d.BookId == book.Id && d.State == true);
foreach (var directory in directories)
{
if (directory.Len == null)
{
//异步下载章节
await Task.Run(()=> { DownloadText(directory, book); });
}
}
}
/// <summary>
/// 下载章节内容
/// </summary>
/// <param name="directory"></param>
public bool DownloadText(DirectoryModel directoryModel, BookModel book)
{
DownLoadTextChanging?.Invoke(book, null);
string html = HttpHelper.DownloadHtml(directoryModel.Url);
if (html == null)
{
DownLoadTextChanged?.Invoke(book, null);
return false;
}
else
{
string bodyHtml = HtmlHelper.HtmlTrim(html, book.TextHead, book.TextTail);
HtmlToText convert = new HtmlToText();
string text = convert.Convert(bodyHtml);
//这个是有缓存查询,Ef的Find
var updateDir = directory.Find<DirectoryModel>(directoryModel.Id);
updateDir.Len = text.Length;
updateDir.Text = text;
updateDir.ActiveTime = DateTime.Now;
//更新章节
directory.Update(updateDir);
directory.SaveChanges();
DownLoadTextChanged?.Invoke(book, null);
return true;
}
}
System.InvalidOperationException:“对数据库所做的更改已成功提交,但在更新对象上下文时出错。此 ObjectContext 可能处于不一致状态。内部异常消息: 保存或接受更改失败,因为类型“FictionsDownload.Model.DirectoryModel”的多个实体具有相同的主键值。请确保显式设置的主键值是唯一的。请确保在数据库和实体框架模型中正确配置了数据库生成的主键。将 Entity Designer 用于 Database First/Model First 配置。将 "HasDatabaseGeneratedOption" Fluent API 或 "DatabaseGeneratedAttribute" 用于 Code First 配置。”
var updateDir = directory.Find<DirectoryModel>(directoryModel.Id);
updateDir.Len = text.Length;
updateDir.Text = text;
updateDir.ActiveTime = DateTime.Now;
//更新章节
directory.Update(updateDir);
directory.SaveChanges();
Parallel.ForEach(list, new ParallelOptions() {MaxDegreeOfParallelism=20 }, x => Console.WriteLine(x.Name));
var r = (from ny in 获取可报的项目(Nian1, Yue1, Nian2, Yue2, LeiXing).AsParallel()
from dw in 获取单位列表().AsParallel()
let o = 申报项目(BaselienDefination, dw, ny.Item1, ny.Item2, IncludeNull, IncludeTrue, IncludeFalse, IncludeStatus0, LeiXing)
select o).ToList();
这里的 AsParallel 不必你写 for 强10倍嘛。更何况一行 PLinq 可能代替复杂的、你需要写几百行几千行的的复杂业务逻辑。