场景:前几天在项目开发时,有个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. CDQ分治(三维偏序集)

    排序,三关键字 去重 归并排序+树状数组 #include<bits/stdc++.h> using namespace std; #define re register int cons ...

  2. 【题解】[LuoguP3503]「BZOJ2086」[POI2010] Blocks

    题目描述 给出N个正整数a[1..N],再给出一个正整数k,现在可以进行如下操作:每次选择一个大于k的正整数a[i],将a[i]减去1,选择a[i-1]或a[i+1]中的一个加上1.经过一定次数的操作 ...

  3. 【题解】Luogu P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    题目描述 Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no ...

  4. ZooKeeper集群解析

    ZooKeeper集群解析. 这篇文章中来介绍一下 ZooKeeper 相关的集群角色,还有 ZAB协议,集群的安装在 ZooKeeper入门 中有介绍. 一.ZooKeeper集群中的角色 Lead ...

  5. mycat高可用-安全管理-监控 看这一篇就够了

    ​ 在之前的操作中,我们已经实现了mysql机器的高可用,可以动态切换master,那么如果mycat崩溃了呢?我们应该如何处理呢?所以此时就需要搭建mycat的高可用集群了. ​ 在mycat的权威 ...

  6. 7.4、Horizon部署

    1.horizon介绍: horizon只需要连上keystone即可: 这里在控制节点controller进行操作: 提示:horizon和keystone运行时使用的都是apache软件,如果都装 ...

  7. 4.13、nfs挂载优化及优缺点

    1.硬盘:sas/ssd磁盘,买多块,硬件raid5/raid0,网卡吞吐量要大,至少千兆(多网卡bond0) 2.nfs客户端挂载说明: 文件系统有自己的权限,挂载是建立在文件系统之上的,然后更改挂 ...

  8. Redis 底层数据结构之String

    文章参考:<Redis设计与实现>黄建宏 Redis 的 string 类型底层使用的是 SDS(动态字符串) 实现的, 具体数据结构如下: struct sdshdr { int len ...

  9. Springboot:单元测试日志打印@Slf4j 注解的使用方法

    当自己写日志的时候,肯定需要: private final Logger logger = LoggerFactory.getLogger(LoggerTest.class); 每次写新的类,就需要重 ...

  10. 1.3.7、通过QueryParam匹配

    server: port: 8080 spring: application: name: gateway cloud: gateway: routes: - id: guo-system4 uri: ...