Entity Framework中Remove、Modified实体时,在修改或删除时引发主键冲突的问题
问题:
try
{
string fileId = context.NewsT.Where(t => t.Id == Model.Id).FirstOrDefault().FileId;
string filePath = context.FilesT.Where(t => t.Id == fileId).FirstOrDefault().Filepath;
var file = context.FilesT.FirstOrDefault(m => m.Id == fileId);
context.NewsT.Remove(Model);
context.FilesT.Remove(file);
var i = context.SaveChanges();
//删除文件
FilesHelper.DeleteFiles(ConfigHelper.GetSectionValue("filePath") + filePath);
result.ErrorCode = "1";
result.Message = "API调用成功";
httpResponseMessage.StatusCode = HttpStatusCode.OK;
}
catch (Exception e)
{
result.ErrorCode = "-999";
result.Message = e.Message;
httpResponseMessage.StatusCode = HttpStatusCode.InternalServerError;
}
ERROR:Attaching an entity of type 'Entity.TableClass' 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.
原因:主键冲突了
解决方案:
将 string fileId = context.NewsT.Where(t => t.Id == Model.Id).FirstOrDefault().FileId;修改为 string fileId = context.NewsT.AsNoTracking().Where(t => t.Id == Model.Id).FirstOrDefault().FileId;//AsNoTracking是预加载,因为ef本身是懒加载(延迟加载),所以如果这里不用预加载,到执行修改时会报model.id冲突错误,所以要用预加载
Entity Framework中Remove、Modified实体时,在修改或删除时引发主键冲突的问题的更多相关文章
- UPDATE 时主键冲突引发的思考【转】
假设有一个表,结构如下: root::> create table t1 ( -> id int unsigned not null auto_increment, ', -> pr ...
- Entity Framework 教程——Entity Framework中的实体类型
Entity Framework中的实体类型 : 在之前的章节中我们介绍过从已有的数据库中创建EDM,它包含数据库中每个表所对应的实体.在EF 5.0/6.0中,存在POCO 实体和动态代理实体两种. ...
- 关于Entity Framework中的Attached报错的完美解决方案终极版
之前发表过一篇文章题为<关于Entity Framework中的Attached报错的完美解决方案>,那篇文章确实能解决单个实体在进行更新.删除时Attached的报错,注意我这里说的单个 ...
- 关于Entity Framework中的Attached报错相关解决方案的总结
关于Entity Framework中的Attached报错的问题,我这里分为以下几种类型,每种类型我都给出相应的解决方案,希望能给大家带来一些的帮助,当然作为读者的您如果觉得有不同的意见或更好的方法 ...
- Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading
Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...
- Entity Framework入门教程(10)---离线场景保存和删除实体/实体图集
离线场景保存和删除实体/实体图集 这一节的内容是在离线场景中保存实体和实体图集 在离线场景中,当我们保存一个离线的实体图集或一个单独的离线实体时,我们需要做两件事.首先,我们要把实体附加到新的上下文中 ...
- Entity Framework中的几种加载方式
在Entity Framework中有三种加载的方式,分别是延迟加载,自动加载和显示加载.下面用一个例子来说明:现在有两个表,一个是资料表(Reference),另外一个表是资料分类表 ...
- Entity Framework中DbContext结合TransactionScope提交事务的正确方式
问: I would like know what is the best possible way to implement transactions with DBContext. In part ...
- [转]在Entity Framework中使用LINQ语句分页
本文转自:http://diaosbook.com/Post/2012/9/21/linq-paging-in-entity-framework 我们知道,内存分页效率很低.并且,如果是WebForm ...
随机推荐
- 【Linux】在docker上部署grafana+zabbix监控实录
------------------------------------------------------------------------------------------------- ...
- leetcode 940. 不同的子序列 II (动态规划 ,字符串, hash,好题)
题目链接 https://leetcode-cn.com/problems/distinct-subsequences-ii/ 题意: 给定一个字符串,判断里面不相同的子串的总个数 思路: 非常巧妙的 ...
- SAP 修改表和表中数据
平时修改表中数据的方式有一下几种: 1.一般就是通过SE11或者是SE16进去,找到那条记录,然后将模式变成EDIT,然后修改保存. 2.通过SQL语句在程序中实现数据库表的修改操作 3.通过SE16 ...
- SAP 中session和外部断点设置的区别
1 Session Breakpoints:只在当前user session的所有main session中有效 2 External Breakpoints 在abap editor或事务SICF中 ...
- 1.2V升压5V和2.4V升压5V芯片,适用于镍氢电池产品
一节镍氢电池1.2V升压5V电路芯片,两节镍氢电池2.4V升压到5V的电源芯片 PW5100具有宽范围的输入工作电压,从很低的0.7V到5V电压. PW5100输出电压固定3V,3.3V,5V可选固定 ...
- 基于循环队列的BFS的原理及实现
文章首发于微信公众号:几何思维 1.故事起源 有一只蚂蚁出去寻找食物,无意中进入了一个迷宫.蚂蚁只能向上.下.左.右4个方向走,迷宫中有墙和水的地方都无法通行.这时蚂蚁犯难了,怎样才能找出到食物的最短 ...
- 超微服务器重置ipmi登录密码
超微服务器的ipmi登录密码不对,需要重置但是bios内并没有找到可以设置的选项. 以下是解决办法: 安装IPMITOOLyum install ipmitool -y 执行以下命令加载模块:modp ...
- 初识JavaScript和jQuery
JavaScript 1.特性 ①脚本语言.JavaScript是一种解释型的脚本语言,C.C++.Java等语言先编译后执行, 而JavaScript是在程序的运行过程中逐行进行解释. ②基于对象. ...
- hasOwnProperty和 ... in ...的区别
hasOwnProperty() 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键).https://developer.mozilla.org/zh-CN/docs ...
- Lambda 表达式 学习
最近几天在学习Lambda,给我的理解就是一个匿名函数的升级版,代码极大可能的简洁了很多,不需要像以前一样必须使用众多的代码才能实现相关功能. 慢慢积累学习,将Java 8的相关知识进行一个学习. 用 ...