EF Core tracking entities with same id multiple times

change-tracking entity-framework entity-framework-core entity-framework-core-2.1

Question

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

1
0
8/23/2018 9:57:04 AM

Popular Answer

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.

0
9/5/2019 7:34:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow