场景:前几天在项目开发时,有个bug经常出现,今天花了一整天,终于把它解决了。记录一下解决流程。

解决方法:

  主要报错的地方在添加的部分:

 1 foreach (var requestProperty in request.Properties)
2 {
3 UnitWork.Add(new Relevance
4 {
5 Key = Define.ROLEDATAPROPERTY,
6 FirstId = request.RoleId,
7 SecondId = request.ModuleCode,
8 ThirdId = requestProperty,
9 OperateTime = DateTime.Now
10 });UnitWork.save();
11 }

  这里的UnitWork(事务类)的Add()方法主要实现“上下文”的以下代码:

1 _context.Set<T>().Add(entity);

  而Save()实现以下代码:

1 _context.SaveChanges();

  我查了半天的问题,发现报错都是:An error occurred while updating the entries. See the inner exception for detail。同时我打印了是哪个实体类的问题,发现每次实体类都不一样。由于在这之前做了删除的操作,一开始我以为是删除的操作,可能是数据库中数据没删干净就开始添加,或者还没删完就开始添加,导致上下文对同一数据操作。后来跟踪了一下断点,查看一下数据库,发现删除的数据和添加的数据没有关系。然后我猜会不会是UnitWork的问题,于是我用了Repository的Add(),其代码如下;

1  public void Add(T entity)
2 {
3 _context.Set<T>().Add(entity);
4 _context.SaveChanges();
5 _context.Entry(entity).State = EntityState.Detached;
6 }

  和unitWork不同的是,这里的Add()方法中多加了一句:对上下文实体类状态的操作。运行,发现成功了,没有报错。原来是Entry.State的关系。

原理:

  先介绍一下实体的State:

Unchanged 未修改。在加载到上下文后未修改
Modified 已修改。已经修改,但是没有使用savechange()
Deleted 已删除。从上下文中删除了对象,但是没有使用savechange()
Added 新添加。对象已经添加到上下文,但没有使用但是没有使用savechange()
Detached 游离态。对象存在,但不被上下文跟踪

  以我个人的理解,那么添加数据的过程应该为:

        1.先添加一个新的对象,此时是状态为Detached,和数据库,上下文都没关系;

        2.采用上下文的Add()添加对象,对象已经添加到上下文,此时状态为Added,但是没有savechange(),数据库中没有该数据;

        3.上下文的savechange(),即同步数据库和上下文。此时该数据状态为Unchanged,数据库中有该数据;

        4.若把对象的State改为Detached,则上下文中没有该对象。不然,对象的State为Unchanged,存在于上下文中。

  因此,我猜测是在UnitWork添加的时候,在上下文中依旧有数据对象,然后在对其操作时(我这里时删除数据)导致不能更新数据。但是具体为什么会报错,不是很理解,希望可以讨论学习一下。

添加数据时报错:An error occurred while updating the entries. See the inner exception for detail。的更多相关文章

  1. An error occurred while updating the entries. See the inner exception for details.

    EF插入或更新数据时出现错误提示:An error occurred while updating the entries. See the inner exception for details.的 ...

  2. EF更新时出错,An error occurred while updating the entries. See the inner exception for details

           在使用EF进行更新数据时出错,报出的异常是 "An error occurred while updating the entries. See the inner excep ...

  3. 使用maven时报错An error occurred while filtering resources

    解决办法:右键项目-->maven-->update project   .

  4. C# an error has occurred while updating the entries.see the log file

    message:An error occurred while updating the entries. See the inner exception for details. C# 在执行插入方 ...

  5. pycharm添加wordcloud模块时报错:error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    windows 7 32bit python3.6.3 32bit pycharm2018社区版 32bit 问题说明: 添加wordcloud模块时报错:error: Microsoft Visua ...

  6. MyEclipse for Spring启动时报错"An internal error occurred during: 'Updating indexes'.Java heap space"的解决办法

    问题 MyEclipse for Spring在启动时,报如下错误:An internal error occurred during: 'Updating indexes'.Java heap sp ...

  7. MySQL 导入外部数据时报错:1153: Got a packet bigger than 'max_allowed_packet' 解决方案

    MySQL 导入外部数据时报错:1153: Got a packet bigger than 'max_allowed_packet' 解决方案 zoerywzhou@163.com http://w ...

  8. apache include 文件包含引用的方法 报错 [an error occurred while processing this directive]

    今天遇到在某平台买的虚拟主机服务器不支持    下面的这样的写法 <!--#Include file="/templets/2013new/header.htm"--> ...

  9. c#保存datagridview中的数据时报错 “动态SQL生成失败。找不到关键信息”

    ilovejinglei 原文 C#中保存datagridview中的数据时报错"动态SQL生成失败.找不到关键信息" 问题描述     相关代码 using System; us ...

随机推荐

  1. 有效Ajax案例

    <script>$(document).ready(function(){ $("input:submit").click(function(){ $.ajax({ t ...

  2. Golang学习(用代码来学习) - 第三篇

    type Books struct { title string author string subject string id int } /** 结构体的学习 */ func struct_tes ...

  3. 我对SpringMVC的浅见

    之前在学校没接触框架这东西之前只接触过MVC的model1和model2,而真正接触SpringMVC的时候是在一年前,在学习过程中,我这才意识到SpringMVC大大简化了以前的开发工程,到了社会上 ...

  4. Arduino库和STM32的寄存器、标准库、HAL库、LL库开发比较之GPIO

    标题: Arduino库和STM32的寄存器.标准库.HAL库.LL库开发比较之GPIO 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#Arduino,#STM32,#库,#开发 ...

  5. 痞子衡嵌入式:以i.MXRT1xxx的GPIO模块为例谈谈中断处理函数(IRQHandler)的标准流程

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是以i.MXRT的GPIO模块为例谈谈中断处理函数(IRQHandler)的标准流程. 在痞子衡旧文 <串口(UART)自动波特率识 ...

  6. 使用远程Docker进行集成测试

    目录 需求背景 使用docker进行环境搭建 以中心化的docker server改进集成测试 Docker Server远程链接配置 Testcontainers 框架 Testcontainers ...

  7. 关于 C#的一些记录

    1, 注意: 使用Linq to Sql 查询数据库的时候,进行where 判断需要注意.我使用的EF,以下为我的记录使用Contain 需要 使用 *.Contains("" + ...

  8. 使用Flash Builder 4.6出现 新建配置 失败 java.lang.NullPointerException错误

    当看到这个错误的时候有点莫名奇妙的感觉,随后的第一反应是: 这跟我前些天安装的java的jre 1.8 有没有关联性.修改了设定,方法如下 "运行" -> "外部工 ...

  9. scRNAseq benchmark 学习笔记

    背景 把早年没填完的坑(单细胞测序的细胞类型鉴别)给重新拾起来 其Github描述的基本情况: 作者并不对单个分类器进行说明,统一包装在benchmark工程里,还建立了docker容器 但说明了在s ...

  10. JAVA 类修饰符

    JAVA类的修饰符主要有public,default,protected,private,final,abstract,static 其中外部类中用到的只有public,final,abstract或 ...