question

DavidCraig-4984 avatar image
0 Votes"
DavidCraig-4984 asked DanielZhang-MSFT edited

Having trouble with foreign key in entity, can't track because the FK is already part of another object

Scenario: I have an entity that is central to the business, let's call it Company. Entity Foo has FK CompanyID, as does entity Bar.

If, within the same context, I retrieve Foo and create an instance of Bar, then try _context.Foo.Update(instanceOfFoo), then I get an InvalidOperationException:

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

CompanyID is part of many entities, removing it is a non-starter.

I'm new-ish to EF Core, and would appreciate any suggestions.

dotnet-entity-framework-core
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Will you be able to share a sample of your code, please. This is because of the Tracking Behavior of EF.

In most cases you should be able to get around this by doing .AsNoTracking() when retrieving.


0 Votes 0 ·

I can't share real code, unfortunately. I'm aware of AsNoTracking(), and have used it other places. The issue here is that I'm not accessing the context directly, but rather through a manager class, and while it's possible to modify that class to not track, this has further ramifications.

I guess what I'm really wondering is, is there a way to say 'don't track the FK relationships if the foreign entity doesn't change?'



1 Vote 1 ·

1 Answer

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered DanielZhang-MSFT edited

Hi DavidCraig-4984,
According to your description, maybe your context is being shared by multiple requests which means that the entity you're editing has been tracked already.
You can try to detach an object that has been attached to the context via setting the state to Detached.
Code likes below:

 Context.Entry(entity).State = EntityState.Detached

If you want to load entities from the database without fully attaching them to the context (without change tracking), you can use AsNoTracking as JaliyaUdagedara saide.
Here are some similar threads you can refer to.
The instance of entity type cannot be tracked because another instance with the same key value for {'Id'} is already being tracked
instance of entity type cannot be tracked because another instance with same key value is tracked
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you both. Detaching the entity didn't work, but this conversation and the links helped me identify some newly added code that was in fact sharing the context, so that has pointed me in the right direction.

Again, appreciate your time,
--David

1 Vote 1 ·

Hi @DavidCraig-4984 ,
You are welcome.

Best Regards
Daniel Zhang

0 Votes 0 ·