文章内容
2017/12/24 17:30:49,作 者: 黄兵
存储区更新、插入或删除语句影响到了意外的行数(0)
最近使用Entity Framework做更新的时候,出现了如下错误:
存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。有关了解和处理乐观并发异常的信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=472540。
代码如下:
using (UnitOfWork uow = new UnitOfWork()){var ZhiHuAll = uow.ZhiHuExtendRepository.GetAll();foreach (var item in ZhiHuAll.ToList()){string newFileName = Guid.NewGuid() + Path.GetExtension(item.ImageUrl);//如果目录不存在就新建一个CreateDirectory ImgDr = new CreateDirectory();ImgDr.CreatePath(FilePath);if(item.Id>16){await Client.DownloadFileTaskAsync(item.ImageUrl, FilePath + newFileName);}Zhihu_Extend ZhihuExtend = new Zhihu_Extend();ZhihuExtend.Image_source = item.Image_source;ZhihuExtend.ImageUrl = newFileName;ZhihuExtend.Js_Url = item.Js_Url;ZhihuExtend.Css_Url = item.Css_Url;ZhihuExtend.Share_Url = item.Share_Url;uow.ZhiHuExtendRepository.Update(ZhihuExtend);uow.SaveChanges();}}
产生问题的原因:
实体类主键未赋值。
解决方案:
using (UnitOfWork uow = new UnitOfWork()){var ZhiHuAll = uow.ZhiHuExtendRepository.GetAll();foreach (var item in ZhiHuAll.ToList()){string newFileName = Guid.NewGuid() + Path.GetExtension(item.ImageUrl);//如果目录不存在就新建一个CreateDirectory ImgDr = new CreateDirectory();ImgDr.CreatePath(FilePath);if(item.Id>16){await Client.DownloadFileTaskAsync(item.ImageUrl, FilePath + newFileName);}Zhihu_Extend ZhihuExtend = new Zhihu_Extend();ZhihuExtend.Image_source = item.Image_source;ZhihuExtend.ImageUrl = newFileName;ZhihuExtend.Js_Url = item.Js_Url;ZhihuExtend.Css_Url = item.Css_Url;ZhihuExtend.Share_Url = item.Share_Url;ZhihuExtend.Id = item.Id;uow.ZhiHuExtendRepository.Update(ZhihuExtend);uow.SaveChanges();}}
评论列表