Repository模式中,Update和Delete总是失败及其解析(报错“another entity of the same type already has the same primar”)

转载 2015年11月19日 11:57:29

在Repository模式中,我的Update方法总是无法更新实体,这个非常郁闷,Update方法如下:

   1:  public virtual void Update(T entity)
   2:          {
   3:              try
   4:              {
   5:                  if (entity == null) throw new ArgumentNullException("实体类为空");
   6:                  Context.Entry(entity).State = EntityState.Modified;
   7:                  //Context.SaveChanges();
   8:              }
   9:              catch (DbEntityValidationException dbex)
  10:              {
  11:                  var msg = string.Empty;
  12:                  foreach (var validationErrors in dbex.EntityValidationErrors)
  13:                      foreach (var validateionError in validationErrors.ValidationErrors)
  14:                          msg += string.Format("Property:{0} Error:{1}", validateionError.PropertyName, validateionError.ErrorMessage);
  15:   
  16:                  var fail = new Exception(msg, dbex);
  17:                  throw fail;
  18:              }
  19:          }

 

看上去是没有任何问题的代码,一旦有实体更新的时候,总会出现如下的错误提示:

Attaching an entity of type 'TinyFrame.Data.DomainModel.t_user_application' 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.

看字面意思,好像是我的EntityState设置不正确导致的,虽然我尝试过重新设置几次EntityState,但是仍旧无法解决我的问题。

然后实在找不出原因,就利用关键字 “ EF Repository Update ”在Google上面搜集,果然找到一篇文章:Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application (12 of 12),其中有一段话,提出了问题的所在:

 

This happened because of the following sequence of events:
 
    The Edit method calls the ValidateOneAdministratorAssignmentPerInstructor method, which retrieves all departments that have Kim Abercrombie as their administrator. That causes the English department to be read. As a result of this read operation, the English department entity that was read from the database is now being tracked by the database context.
    The Edit method tries to set the Modified flag on the English department entity created by the MVC model binder, which implicitly causes the context to try to attach that entity. But the context can't attach the entry created by the model binder because the context is already tracking an entity for the English department.
 
One solution to this problem is to keep the context from tracking in-memory department entities retrieved by the validation query. There's no disadvantage to doing this, because you won't be updating this entity or reading it again in a way that would benefit from it being cached in memory.
 

问题的原因如下:

在Context对象中,已经hold住了一个需要操作的对象,当我们把EntityState修改成modified的时候,Context会再次去加载那个操作对象,但是这样加载是无法成功的,因为当前已经存在一个对象了,再加载会导致重复,然后抛出失败的错误。

解决方法很简单,就是在展示列表的时候,利用AsNoTracking将Hold住的对象释放掉即可。我们修改代码如下:

 
 
   1:   public virtual T Get(Expression<Func<T, bool>> where)
   2:          {
   3:              return Dbset.Where(where).AsNoTracking().FirstOrDefault<T>();
   4:          }
   5:   
   6:          public virtual IQueryable<T> GetMany(Expression<Func<T, bool>> where)
   7:          {
   8:              return Dbset.Where(where).AsNoTracking();
   9:          }

 

 

然后提交,OK,问题解决。

 

=============================Update 2014.09.19======================

看到评论中有朋友虽然按照上述方法,但是仍然无法解决这一问题。原因是在Context中还保留有当前实体的副本所致,这里只要我们将实体副本从内存中完全移除,就可以了。

复制代码
   //用于监测Context中的Entity是否存在,如果存在,将其Detach,防止出现问题。
        private Boolean RemoveHoldingEntityInContext(T entity)
        {
            var objContext = ((IObjectContextAdapter)_context).ObjectContext;
            var objSet = objContext.CreateObjectSet<T>();
            var entityKey = objContext.CreateEntityKey(objSet.EntitySet.Name, entity);

            Object foundEntity;
            var exists = objContext.TryGetObjectByKey(entityKey, out foundEntity);
          
            if (exists)
            {
                objContext.Detach(foundEntity);
            }

            return (exists);
        }
复制代码

然后在Repository中,在进行更新和删除之前,运行一下即可:

复制代码
  public T Remove(T entity)
        {
            try
            {
                RemoveHoldingEntityInContext(entity);

                _context.DbSet<T>().Attach(entity);
                return _context.DbSet<T>().Remove(entity);
            }
            catch (DbEntityValidationException dbex)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbex.EntityValidationErrors)
                    foreach (var validateionError in validationErrors.ValidationErrors)
                        msg += string.Format("属性:{0} 错误:{1}", validateionError.PropertyName, validateionError.ErrorMessage);
                var fail = new Exception(msg, dbex);
                throw fail;
            }
        }

        public T Update(T entity)
        {
            try
            {
                RemoveHoldingEntityInContext(entity);

                var updated = _context.DbSet<T>().Attach(entity);
                _context.DbContext.Entry(entity).State = EntityState.Modified;
                return updated;
            }
            catch (DbEntityValidationException dbex)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbex.EntityValidationErrors)
                    foreach (var validateionError in validationErrors.ValidationErrors)
                        msg += string.Format("属性:{0} 错误:{1}", validateionError.PropertyName, validateionError.ErrorMessage);
                var fail = new Exception(msg, dbex);
                throw fail;
            }
        }
复制代码


本文源地址:http://www.cnblogs.com/scy251147/p/3688844.html

另有一篇简短的文章:http://www.cnblogs.com/ben121011/p/4367234.html

相关文章推荐

EF Attach时已存在的处理方式

如果我们在先前的步骤中读取过数据,如 var list = db.Model.ToList(); 之后再,附加 var o = new Model { Id = 1 }; db.Model.A...
  • killcwd
  • killcwd
  • 2016年06月20日 13:41
  • 1376

使用EF6和MVC5实现一个简单的选课系统--EF6的高级用法(12/12)

先贴出来英文的,抽空翻译! The Contoso University sample web application demonstrates how to create ASP.NET MV...

MVC5 Entity Framework学习之Entity Framework高级功能

在之前的文章中,你已经学习了如何实现每个层次结构一个表继承。本节中你将学习使用Entity Framework Code First来开发ASP.NET web应用程序时可以利用的高级功能。 在本节中...

Powershell学习之道-文件夹共享及磁盘映射

在Linux环境下,我们很轻易就能得心应手地通过命令操作一切事物,在Windows下,Powershell也算是后起之秀,提供大量的cmdlet以及c#的横向拓展。下面将由小编带领大家通过Powers...

PowerShell 数组(Array)

当一个命令执行返回多个结果的时候,PowerShell 会自动将结果集转换成数组存储,把每一行作为一个元素。以下本人测试了一些基本的数组操作。 PowerShell 当前版本: ...

hibernate在saveOrUpdate时,update报错:a different object with the same identifier value was already assoc

1 原因:网上说是,在hibernate中同一个session里面有了两个相同标识但是是不同实体。 2 我找出的解决方法是:getSession().clear(); //在dao层把sessi...

Double Delete caused by 2 shared library has global object of same name, 2) 1st glance

The 1st question I have is what causes the double deletion? With the help of gdb, it's clear ...

spring(错误三) 报错" a different object with the same identifier value was already associated ..."

错误信息如下: org.springframework.dao.DuplicateKeyException: a different object with the same identifier ...

hibernate报错a different object with the same identifier value was already associated with the session

以前也遇到过这个异常,当时也不知道怎么做就上网查,这回又再一次的遇到了这个异常了,决心要追根溯源,找到问题的原点。 a different object with the same identifi...

a different object with the same identifier value was already (用hibernate的update方法一次请求多次update时出现的错)

用Hibernate的update时,多次执行回报这个错  a different object with the same identifier value was already assoc...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Repository模式中,Update和Delete总是失败及其解析(报错“another entity of the same type already has the same primar”)
举报原因:
原因补充:

(最多只允许输入30个字)