问题的详细描述:

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. CentOS7中_带sqlite3_CGO的golang程序_交叉编译到arm中

    CentOS7中_带sqlite3_CGO的golang程序_交叉编译到arm中 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-10-28. 编写了个golang程序,用到了这个C ...

  2. 持续集成(CI):Jmeter+Ant+Jenkins定时构建

    这里Jenkins的安装部署以及工程项目的整体配置不做赘述,其它博文已经说明,这里主要是赘述Ant的相关配置,build.xml文件配置以及项目中的部分配置 一.build.xml 在Ant的安装目录 ...

  3. SpringCloud之Feign 负载均衡请求超时时间

    版本声明: SpringCloud:Greenwich.SR4 SpringBoot:2.1.9.RELEASE Feign调用服务的默认时长是1秒钟,也就是如果超过1秒没连接上或者超过1秒没响应,那 ...

  4. centos7上安装jdk1.8

    我这里是使用的jdk1.8:由于vmware上的字体太小,所以我使用xShell链接linux系统进行操作的. 准备工作:使用xftp链接linux系统:然后创建个包将linux版本的jdk上传上去: ...

  5. layui table 表格查询无效问题

    [热身话题] 在开发的过程中,大量数据的展示大多采用表格的方式,直观,清晰.在这里,我也使用过一些框架Bootstrap.table ,Dev table ,layui table.本次采用的layu ...

  6. android sdk tools里找不到draw9patch.bat 如何制作.9.png 格式图片

    1.什么是.9.png格式 .9.png格式的图片可以在安卓平台指定拉伸区域,防止图片失真,一般用作背景图. 2.前提 最近项目中要用到.9.png格式图片,想着这个简单使用draw9patch.ba ...

  7. 使用动态SQL处理table_name作为输入参数的存储过程(MySQL)

    关于mysql如何创建和使用存储过程,参考笔记<MySQL存储过程和函数创建>以及官网:https://dev.mysql.com/doc/refman/5.7/en/create-pro ...

  8. diango创建一个app

    创建一个app terminal里执行命令 python manage.py startapp app名称 注册 settings配置 INSTALLED_APPS = [ 'app01', 'app ...

  9. 冒泡排序(C语言)

    # include<stdio.h> int main(void) { int arr[10]={5,4,7,9,2,3,1,6,10,8}; //定义一个位排序的数组 int i; // ...

  10. 30(1).原型聚类---k-means

    原型聚类prototype-based clustering假设聚类结构能通过一组原型刻画. 常见的原型聚类有: k均值算法k-means 学习向量量化算法Learning Vector Quanti ...