简体   繁体   中英

EF Core tracking entities with same id multiple times

I simply want to update an industry property of a client object. To do so, I retrieve the client from the DB using a repository. This would result in my industry (example id:149) being tracked.

            var client = await _clientRepository.GetAsync(request.ClientId, token);
            ind = _db.ChangeTracker.Entries<Industry>().Count();

Then, I update the industry by calling SetIndustry with a new Industry with Id:149 (same as before).

            client.SetIndustry(new Industry(149)); 
            ind = _db.ChangeTracker.Entries<Industry>().Count();

At this point EF Core 2.1 throws an exception.

System.InvalidOperationException: 'The instance of entity type 'Industry' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.

In this class I cannot access the DBContext so i can't play with the change tracker. I was wondering how I could work around this problem?

Thx Seb

I just added AsNoTracking() to my document object when I created my document variable and it worked out. Try doing the same with your client object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM