问题的详细描述:

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.的更多相关文章

  1. 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 ...

  2. Invalid prop: type check failed for prop "XXX". Expected String, got Object.

    项目是Vue的,基于elementUI的后台管理系统. Invalid prop: type check failed for prop "total". Expected Str ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. springboot 工程启动报错之Consider defining a bean of type ‘XXX’ in your configuration.

    一.前言: 使用springboot自动注入的方式搭建好了工程,结果启动的时候报错了!!!,错误如下图: Description: Field userEntityMapper in com.xxx. ...

  7. spring注入时报错::No qualifying bean of type 'xxx.xxMapper'

    做一个小项目,因为有 baseService,所以偷懒就没有写单独的每个xxService接口,直接写的xxServiceImpl,结果在service实现类中注入Mapper的时候,用的 @Auto ...

  8. 报错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; ...

  9. 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错误.....,查找的 ...

随机推荐

  1. Netty面试题和解答(一)

    基础 TCP和UDP的区别? TCP是面向连接的(在客户端和服务器之间传输数据之前要先建立连接),UDP是无连接的(发送数据之前不需要先建立连接) TCP提供可靠的服务(通过TCP传输的数据.无差错, ...

  2. Jupyter修改设置

    下载完anaconda后Jupyter默认目录是用户目录,默认浏览器是IE,让有强迫症的我有点难受,所以把它的默认目录和浏览器修改一下. 首先运行一下jupyter notebook --genera ...

  3. Ajax简单应用之个人简历页面搭建

    1.搭建HTTP静态Web服务器. 代码实现: # 1.导入socket模块 import socket import threading # 创建服务器类 class HttpServerSocke ...

  4. net.sf.json将string转为map

    背景 改一个以前的项目,项目里只有这个包,虽然我想用gson或者fastjson,然而并不想引入新的jar.于是使用这个,特此记录,感觉贼不好用. 实现代码 entity.getData()的值:{a ...

  5. SpringCloudConfig对称加密 yml配置文件while parsing a block mapping

    错误的配置!!! #-----------------db------------------ mybatis: type-aliases-package: com.book.product.pojo ...

  6. Spring基础——配置文件pom.xml,web.xml,ApplicationContext.xml

    Spring配置文件——复制粘贴即用 为了以后兼容SSM框架,直接创建Maven Project,包结构如下图. pom.xml <project xmlns="http://mave ...

  7. c# 保留2位小数 整数时无小数

    对数值保存两位小数,有时是整数时,不需要显示两位小数.例如值为:1.32 保留两位,结果是1.32,值为:2,结果有两种显示,2和2.00 /// <summary> /// 金额 /// ...

  8. 通过pipeline实现jenkins的ci/cd功能

    pipeline是基于groove进行实现的,不过从jenkins官方的说明中,pipeline分为脚本式和声明式,参见链接.经过对两种的比较,个人比较偏向脚本式的方法.也就是 Jenkinsfile ...

  9. 520表白酷炫html

    html表白神器,动态浮动爱心,话不多说,直接看截图吧! 下面附上完整代码.别忘了点个赞哦! <!doctype html> <html> <head> <m ...

  10. Linux第二章-Linux常用命令

    一.Linux常用快捷键 快捷键 作用 Tab 补全文件名或者路径 Ctrl + L 清除屏幕,然后,在最上面重新显示目前光标所在的这一行的内容. Ctrl + C 终止当前进程 Ctrl + D 注 ...