The instance of entity type ‘XXXX’ cannot be tracked because another instance with the same key value for {‘XXXX’} is already being tracked.


While working my EF core Application I was getting this exception message. I fixed this issue like this

Step 1: In Insert/update method make ensure to detach the entity.

_db.Entry(localRopFiles).State = EntityState.Detached;

Step 2: If you are loading data on page load then use AsNoTracking() method

var query = await _db.LocalRopFiles.AsNoTracking().ToListAsync();

I hope this will help some one who is getting this type of exception.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.