在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;
}
}

Repository模式中,Update总是失败及其解析的更多相关文章

  1. Repository模式中,Update总是失败及其解析(转)

    出处:http://www.cnblogs.com/scy251147/p/3688844.html 关于Entity Framework中的Attached报错的完美解决方案终极版 前发表过一篇文章 ...

  2. 使用Repository模式构建数据库访问层

    使用Repository模式构建数据库访问层 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程[二]——使用Repository模式构建数据库访问层 系列导航地址http:// ...

  3. 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【二】——使用Repository模式构建数据库访问层

    系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 在数据访问层应用Repository模式来隔离对领域对象的细节操作是很有意义的.它位于映射层 ...

  4. Entity Framework Repository模式

    Repository模式之前 如果我们用最原始的EF进行设计对每个实体类的“C(增加).R(读取).U(修改).D(删除)”这四个操作. 第一个:先来看看查询,对于实体类简单的查询操作,每次都是这样的 ...

  5. (转)MVC中的Repository模式

    1.首先创建一个空的MVC3应用程序,命名为MyRepository.Web,解决方案命名为MyRepository. 2.添加一个类库项目,命名为MyRepository.DAL,添加一个文件夹命名 ...

  6. MVC中的Repository模式

    1.首先创建一个空的MVC3应用程序,命名为MyRepository.Web,解决方案命名为MyRepository. 2.添加一个类库项目,命名为MyRepository.DAL,添加一个文件夹命名 ...

  7. 在 Laravel 5 中使用 Repository 模式实现业务逻辑和数据访问的分离

    1.概述 首先需要声明的是设计模式和使用的框架以及语言是无关的,关键是要理解设计模式背后的原则,这样才能不管你用的是什么技术,都能够在实践中实现相应的设计模式. 按照最初提出者的介绍,Reposito ...

  8. 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获

    项目开发中的一些注意事项以及技巧总结   1.jquery采用ajax向后端请求时,MVC框架并不能返回View的数据,也就是一般我们使用View().PartialView()等,只能返回json以 ...

  9. MVC架构中的Repository模式 个人理解

    关于MVC架构中的Repository模式   个人理解:Repository是一个独立的层,介于领域层与数据映射层(数据访问层)之间.它的存在让领域层感觉不到数据访问层的存在,它提供一个类似集合的接 ...

随机推荐

  1. Spring和cxf3的整合,以maven的方式

    一.引入cxf3 我这里使用的是最新的版本cxf3.1.8 引入cxf3需要在pom.xml加入如下内容: <dependency> <groupId>org.apache.c ...

  2. java观察者模式的实现

    在看博客里,有个订阅功能,当你订阅后,当博主发布新的博客,你都能收到消息.这是如何实现的?是不是后台有个线程在不停的轮询?如果是这样的话,显然太耗资源,如果当博客在发布时,找到所有的订阅者,然后循环的 ...

  3. oracle 判断中文函数

    create or replace function func_chinese(  p_str     in varchar2,     -- 输入的字符串  p_code    in varchar ...

  4. web开发必备-网络基础知识---记录一下

    1.osi 7层网络模型 2.socket 套接字编程 PS:一个从事web开发的人来说,我们是使用http协议来和服务器来进行交互.后面会详细的分析这个过程.

  5. Intellij Idea 15 旗舰版 破解

    转自:http://my.oschina.net/nyp/blog/533991(良心呀,真的好使) 注册方法:   注册码可以沿用14的,只是在 注册时选择 License server ,填 ht ...

  6. Linux 求文件交集 差集等

    使用comm命令 假设两个文件FILE1和FILE2用集合A和B表示,FILE1内容如下: a b c e d a FILE2内容如下: c d a c 基本上有两个方法,一个是comm命令,一个是g ...

  7. mac系统如何关闭root账户

    第一步:系统偏好设置 ->用户与群组 第二步:登录选项 ->解锁 ->单击网络帐户服务器加入 第三步:打开目录实用工具 第四步:菜单栏 ->编辑 ->停用 Root 用户 ...

  8. fast db 学习

    见 http://code.google.com/p/mmdbsolution/source/browse/trunk/+mmdbsolution+--username+SiliangDu1987%4 ...

  9. ch6 影响 MySQLServer 性能的相关因素

    第6章影响 MySQLServer 性能的相关因素 前言: 大部分人都一致认为一个数据库应用系统(这里的数据库应用系统概指所有使用数据库的系统)的性能瓶颈最容易出现在数据的操作方面,而数据库应用系统的 ...

  10. struts2:数据校验,通过Action中的validate()方法实现校验(续:多业务方法时的不同验证处理)

    前文:struts2:数据校验,通过Action中的validate()方法实现校验,图解 如果定义的Action中存在多个逻辑处理方法,且不同的处理逻辑可能需要不同的校验规则,在这种情况下,就需要通 ...