问题:

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实体时,在修改或删除时引发主键冲突的问题的更多相关文章

  1. UPDATE 时主键冲突引发的思考【转】

    假设有一个表,结构如下: root::> create table t1 ( -> id int unsigned not null auto_increment, ', -> pr ...

  2. Entity Framework 教程——Entity Framework中的实体类型

    Entity Framework中的实体类型 : 在之前的章节中我们介绍过从已有的数据库中创建EDM,它包含数据库中每个表所对应的实体.在EF 5.0/6.0中,存在POCO 实体和动态代理实体两种. ...

  3. 关于Entity Framework中的Attached报错的完美解决方案终极版

    之前发表过一篇文章题为<关于Entity Framework中的Attached报错的完美解决方案>,那篇文章确实能解决单个实体在进行更新.删除时Attached的报错,注意我这里说的单个 ...

  4. 关于Entity Framework中的Attached报错相关解决方案的总结

    关于Entity Framework中的Attached报错的问题,我这里分为以下几种类型,每种类型我都给出相应的解决方案,希望能给大家带来一些的帮助,当然作为读者的您如果觉得有不同的意见或更好的方法 ...

  5. Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading

    Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...

  6. Entity Framework入门教程(10)---离线场景保存和删除实体/实体图集

    离线场景保存和删除实体/实体图集 这一节的内容是在离线场景中保存实体和实体图集 在离线场景中,当我们保存一个离线的实体图集或一个单独的离线实体时,我们需要做两件事.首先,我们要把实体附加到新的上下文中 ...

  7. Entity Framework中的几种加载方式

            在Entity Framework中有三种加载的方式,分别是延迟加载,自动加载和显示加载.下面用一个例子来说明:现在有两个表,一个是资料表(Reference),另外一个表是资料分类表 ...

  8. Entity Framework中DbContext结合TransactionScope提交事务的正确方式

    问: I would like know what is the best possible way to implement transactions with DBContext. In part ...

  9. [转]在Entity Framework中使用LINQ语句分页

    本文转自:http://diaosbook.com/Post/2012/9/21/linq-paging-in-entity-framework 我们知道,内存分页效率很低.并且,如果是WebForm ...

随机推荐

  1. MyBatis 查询的时候属性名和字段名不一致的问题

    目录 问题 解决方案:resultMap 问题 当我们数据库中的字段和实体类中的字段不一致的时候,查询会出问题 数据库字段是 pwd id name pwd 1 张三 123456 2 李四 1234 ...

  2. 5.1中repair table

    mysql> repair table xs;+---------+--------+----------+----------+| Table | Op | Msg_type | Msg_te ...

  3. 三十三:WEB漏洞-逻辑越权之水平垂直越权

    水平和垂直越权 水平越权:可以获得同级别用户权限 垂直权限:享受高几个层次的用户权限 解释,原理,检测,利用,防御 通过更换的某个ID之类的身份标识,从而使得A账号获取(修改,删除)B账号的数据,通过 ...

  4. compare-algorithms-for-heapqsmallest

    Compare algorithms for heapq.smallest « Python recipes « ActiveState Code http://code.activestate.co ...

  5. 关于MongoDB的简单理解(二)--Java篇

    一.声明 本文依赖于 MongoDB JVM DRIVERS 4.1 版本编写. 本文案例依赖于 Maven 项目管理工具. 二.本文主要讲解哪些内容? 如何连接到MongoDB 通过TLS/SSL连 ...

  6. 关于MinGW64的调试

    学习的机房电脑能老了,都是xp系统.安装DEV C++后发现为MinGW64. 而我常用编译调试命令为: g++ -g *.cpp -o a gdb a 编译出的程序无法调试. 一直以为,根本就无法调 ...

  7. Oracle数据库查询锁表及解锁

    一.查询哪些表被锁以及查看锁表得会话及操作系统进程ID 其中locked_mode为锁的级别,spid为数据库所在操作系统的进程id select c.sid, c.serial#, c.userna ...

  8. 《》——8幅图图解Java机制

    String对象不可改变的特性 String s = "abcd"; s = s.concat"ef"; equals()与hashCode()方法协作约定 H ...

  9. 单体架构、SOA架构、微服务架构

  10. Spring boot获取getBean

    package com.job.center.quartz.common; import org.springframework.beans.BeansException; import org.sp ...