码迷,mamicode.com
首页 > 其他好文 > 详细

UnitOfWork 更新实体出错解决办法

时间:2014-09-10 13:59:00      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   div   cti   sp   log   

用UnitOfWork进行实体更新的时候,再查询实体一次,再去更新的时候会报如下错误:

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.

 

解决办法如下:

    public virtual async Task<T> GetOne(Expression<Func<T,bool>> where) {
            return await _dbSet.Where(where).AsNoTracking().FirstOrDefaultAsync();
        }

用AsNoTracking不进行跟踪,就可以更新了

记录下

UnitOfWork 更新实体出错解决办法

标签:style   blog   color   io   ar   div   cti   sp   log   

原文地址:http://www.cnblogs.com/flyfish2012/p/3964198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!