Unitofwork update entity error Solution

Source: Internet
Author: User

When unitofwork is used to update an object, the following error is returned when the object is queried again:

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.

 

The solution is as follows:

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

If asnotracking is used, it can be updated without tracking.

Record

Unitofwork update entity error Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.