Attaching an entity of type 'xxx' failed because another entity of the same type already has the same primary key value.
问题的详细描述:
Attaching an entity of type 'xxxxx' 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.
解决方案:
public void Update(T entity)
{
if (entity == null)
{
throw new ArgumentException("entity");
}
if (this.Entry(entity).State == EntityState.Detached)
{
HandleDetached(entity);
}
this.Table.Attach(entity);
this.Entry(entity).State = EntityState.Modified;
this.SaveChanges();
} private bool HandleDetached(T entity)
{
var objectContext = ((IObjectContextAdapter)this).ObjectContext;
var entitySet = objectContext.CreateObjectSet<T>();
var entityKey = objectContext.CreateEntityKey(entitySet.EntitySet.Name, entity);
object foundSet;
bool exists = objectContext.TryGetObjectByKey(entityKey, out foundSet);
if (exists)
{
objectContext.Detach(foundSet);
}
return exists;
}
Attaching an entity of type 'xxx' failed because another entity of the same type already has the same primary key value.的更多相关文章
- The entity type XXX is not part of the model for the current context.
今天遇到了一个奇葩问题,虽然解决了,但还是一脸懵,先附赠一下别人的解决方案:https://www.cnblogs.com/zwjaaron/archive/2012/06/08/2541430.ht ...
- Invalid prop: type check failed for prop "XXX". Expected String, got Object.
项目是Vue的,基于elementUI的后台管理系统. Invalid prop: type check failed for prop "total". Expected Str ...
- Vue报错 type check failed for prop “xxx“. Expected String with value “xx“,got Number with value ‘xx‘
vue报错 [Vue warn]: Invalid prop: type check failed for prop "name". Expected String with ...
- ASP.NET MVC another entity of the same type already has the same primary key value
ASP.NET MVC项目 Repository层中,Update.Delete总是失败 another entity of the same type already has the same pr ...
- System.Security.SecurityException The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception
[15/08/19 00:03:10] [DataManager-7292-ERROR] System.Reflection.TargetInvocationException: Exception ...
- springboot 工程启动报错之Consider defining a bean of type ‘XXX’ in your configuration.
一.前言: 使用springboot自动注入的方式搭建好了工程,结果启动的时候报错了!!!,错误如下图: Description: Field userEntityMapper in com.xxx. ...
- spring注入时报错::No qualifying bean of type 'xxx.xxMapper'
做一个小项目,因为有 baseService,所以偷懒就没有写单独的每个xxService接口,直接写的xxServiceImpl,结果在service实现类中注入Mapper的时候,用的 @Auto ...
- 报错HTTP Status 500 - HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; nested exception is org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.itcast.entity.
报错 type Exception report message HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; ...
- The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....
遇到The type XXX cannot be resolved. It is indirectly referenced from required .class files错误.....,查找的 ...
随机推荐
- 一 、爬虫的认识与http
一 .爬虫的认识与http 互联网应用架构 一般采用c/s架构,b/s架构或者m/s架构 c/s 即 client server 客户端 服务端 b/s 即 browser server 浏览器 服 ...
- 【30天自制操作系统】day01:内存分布图
- Redis 命令执行全过程分析
今天我们来了解一下 Redis 命令执行的过程.我们曾简单的描述了一条命令的执行过程,本篇文章展示深入说明一下,加深大家对 Redis 的了解. 如下图所示,一条命令执行完成并且返回数据一共涉及三部分 ...
- Lucene&Solr框架之第三篇
1.SolrCore的配置 a)schma.xml文件 b)配置中文分析器 2.配置业务域和批量索引导入 a)配置业务域 b)批量索引导入 c)Solrj复杂查询(用Query页面复杂查询.用程序实现 ...
- IS:Introduction Parrot
Ax_What is Linux? "Linux is a family of free and open-source software operating systems based o ...
- Windows 2012 R2 安装RD服务
默认只能同时允许2个用户连接,如果希望更多用户同时连接服务器,需要开启并激活远程桌面服务.参考:https://jingyan.baidu.com/article/9f7e7ec0f5a8686f28 ...
- SQL Server 之事务执行,让语句在事务中执行
BEGIN TRAN BEGIN TRY DELETE FROM dbo.表 INSERT INTO dbo.表( Id, 字段....) SELECTId,字段... F ...
- SpringMVC自学笔记
1.<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 通过ser ...
- jxl.jar下载
jxl.jar是通过java操作excel表格的工具类库,是由java语言开发而成的. 在网上找了很多,不是链接失效,就是csdn上要钱的,所以干脆上传个到自己的博客文件里,方便你们下载. 下载地址: ...
- CF1062F Upgrading Cities
题意 由于这是个\(DAG\),我们考虑拓朴排序,求某个点能到的和能到它的点,这是两个问题,我们可以正反两边拓朴排序,这样就只用考虑它能到的点了 设\(f[x]\)表示\(x\)能到的点数\(+\)能 ...