zoukankan      html  css  js  c++  java
  • UnitOfWork 更新实体出错解决办法

    用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不进行跟踪,就可以更新了

    记录下

  • 相关阅读:
    HDOJ 2076 夹角有多大(题目已修改,注意读题)
    HDOJ 2058 The sum problem
    HDOJ 2099 整除的尾数
    HDOJ 2075 A|B?
    HDOJ 2071 Max Num
    HDOJ 2056 Rectangles
    HDOJ 2055 An easy problem
    HDOJ 2053 Switch Game
    HDOJ 2052 Picture
    P4139 上帝与集合的正确用法
  • 原文地址:https://www.cnblogs.com/flyfish2012/p/3964198.html
Copyright © 2011-2022 走看看