正文
添加数据时报错:An error occurred while updating the entries. See the inner exception for detail。
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
场景:前几天在项目开发时,有个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添加的时候,在上下文中依旧有数据对象,然后在对其操作时(我这里时删除数据)导致不能更新数据。但是具体为什么会报错,不是很理解,希望可以讨论学习一下。