码迷,mamicode.com
首页 > 其他好文 > 详细

【异常】Repository模式,Update方法总是失败

时间:2016-05-08 19:41:48      阅读:615      评论:0      收藏:0      [点我收藏+]

标签:

1、错误信息

技术分享

Attaching an entity of type ‘LawProject.Models.Models.T_Category‘ failed because another entity of the same type already has the same primary key value. This can happen when using the ‘Attach‘ method or setting the state of an entity to ‘Unchanged‘ or ‘Modified‘ if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the ‘Add‘ method or the ‘Added‘ entity state to track the graph and then set the state of non-new entities to ‘Unchanged‘ or ‘Modified‘ as appropriate.

2、原因

详细的看这里:http://www.bubuko.com/infodetail-251170.html

总结原因就是说,在Context对象中,‘Attach 的状态是不可修改状态,我们要把它修改成‘Unchanged‘ or ‘Modified‘,Context不允许这么操作所以抛出错误。

解决方法可以利用AsNoTracking 释放状态

 

return await this.dbset.Where(where).AsNoTracking().FirstOrDefaultAsync();

 

【异常】Repository模式,Update方法总是失败

标签:

原文地址:http://www.cnblogs.com/eryueren/p/5471169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!