你的位置:首页 > 软件开发 > ASP.net > ASP.NET MVC another entity of the same type already has the same primary key value

ASP.NET MVC another entity of the same type already has the same primary key value

发布时间:2015-03-26 20:01:29
ASP.NET MVC项目 Repository层中,Update、Delete总是失败 another entity of the same type already has the same primary key value在项目里的Repository层中的涉及到数据 ...

get='_blank'>ASP.NET MVC项目 Repository层中,Update、Delete总是失败

another entity of the same type already has the same primary key value

在项目里的Repository层中的涉及到数据的update方法总是报错,delete时有时也会报错,报的错误是

Attaching an entity of type 'Model.Diary' 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.

 

按字面意思看,发生异常的原因在于已经存在了一个实体与要进行操作的实体存在相同的主键,相互冲突。

解决方案:

  1. 查询时让EF不要跟踪

    原标题:ASP.NET MVC another entity of the same type already has the same primary key value

    关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。