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错误.....,查找的 ...
随机推荐
- 13-Node.js学习笔记-MongoDB
数据库相关概念 在一个数据库软件最终可以包含多个数据仓库,在每个数据仓库中可以包含多个数据集合,每个数据集合中可以包含多条文档(具体的数据) database: 数据库,mongoDB数据库软件中可以 ...
- [CodeForces - 1225C]p-binary 【数论】【二进制】
[CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...
- k8s采坑记 - 证书过期之kubeadm重新生成证书
重新生成证书 证书备份 cp -rp /etc/kubernetes /etc/kubernetes.bak 移除过期证书 rm -f /etc/kubernetes/pki/apiserver* r ...
- CMake工程找不到相应的cuDNN版本的问题
(1) 去官网下载相应的版本,因为电脑之前安装的是 CUDA8. ,找跟 CUDA 版本兼容的 cuDNN 下载即可,我选择的是 cuDNN v7.(Deb) 和 cuDNN v7.1.4 Deve ...
- 修改Android源码实现原生应用双开,应用多开
1. 准备 把某系统双开的两个app的信息进行对比 1.1 目录的对比 1.1.1 data目录对比 原应用: /data/user/0/com.luoyesiqiu.crackme/files 被复 ...
- Microsemi Libero使用技巧——使用第三方编辑器Notepad++
前言 与Xilinx的ISE和Altera的Quartus一样,Microsemi的编辑器也支持指定第三方编辑器. Microsemi自带的编辑器,没有自动补全功能,也不支持中文注释,非常不好用,为了 ...
- IPFS学习-分布式哈希表DHT
Distributed Hash Tables(DHT) 分布式哈希表是一个分布式的键值对存储结构.在IPFS网络中,每一个节点都维护一个DHT的子集.当节点接受到一个请求.该节点要么直接回复,要么通 ...
- Centos7通过yum跟源码编译安装Nginx
源码编译安装 http://nginx.org/en/download.html 到官网下载,然后用XFTP上传到root目录 把文件解压出来 tar -zxvf nginx-1.16.0.tar.g ...
- JS---DOM---为元素绑定事件的引入,为元素绑定多个代码,兼容代码
1. 为元素绑定事件的引入: 用src直接绑定多个,只实现最后一个(programmer2.js) <input type="button" value="按钮&q ...
- JS---DOM---设置和获取---标签内容和文本内容
设置和获取---标签内容和文本内容 总结---设置: 使用innerText主要是设置文本的, 设置标签内容, 是没有标签的效果的 innerHTML是可以设置文本内容 innerHTML主要的作 ...