C
C#14mo ago
Emre

❔ Entity framework, tracking same Id multiple times. Need help

No description
21 Replies
Emre
EmreOP14mo ago
getting following error: Could not insert Azure DevOps projects and work items into the database. Exception: The instance of entity type 'WorkItem' cannot be tracked because another instance with the same key value for {'WorkItemId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values. Im a beginner in entityframework
Brainiac V
Brainiac V14mo ago
Why are you trying to update an entity like this in the first place at all? Where did you learn that from?
Emre
EmreOP14mo ago
I usually work with TSQL so i didnt really learn it really can you tell me what im doing wrong @Brainiac V
Brainiac V
Brainiac V14mo ago
So why not reading through examples or tutorials?
Emre
EmreOP14mo ago
I always find it hard to go through general tutorials. I try to watch some videos to get a general understanding and once I get stuck, i learn by solving them. If you have any tutorial that fits my scenario more specifically id appreciate if you could send it
Brainiac V
Brainiac V14mo ago
So the first thing you need to make sure is through debugging is that you don't have two WorkItem entities with the same WorkItemId Because that's what EF Core is complaining about right now
Emre
EmreOP14mo ago
I cant have multiple workitems with same id because these are fetched from azure devops which is always incremented automatically. It must be something with my existingWorkItem when i query the database I read alittle about entity framework tracking. So I guess its tracking the workitem while trying to update the fields for that same workitemId
Brainiac V
Brainiac V14mo ago
But the real problem here seems to be, that you first fetch existing items without tracking by EF Core, then you modify them, then you attach them to the context and change the state. The issue is probably that at the moment in your application you already have that item tracked by EF Core and that then causes issues. So to solve it, you can fetch the original items in a tracked way and modify them, so EF Core knows about it
Emre
EmreOP14mo ago
I added the no tracking in order to bypass my error. Which also didn't work
Brainiac V
Brainiac V14mo ago
So you can also remove this code:
_mainDbContext.WorkItems.Attach(existingWorkItem);
_mainDbContext.Entry(existingWorkItem).State = EntityState.Modified;
_mainDbContext.WorkItems.Attach(existingWorkItem);
_mainDbContext.Entry(existingWorkItem).State = EntityState.Modified;
Also don't use SaveChangesAsync as long as you really don't know what the consequences are
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Brainiac V
Brainiac V14mo ago
So to solve your problem, let EF Core track your fetched items properly from fetching the data Then modify your data and just call save without async
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Brainiac V
Brainiac V14mo ago
Then none of the problems above should happen
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Emre
EmreOP14mo ago
that wouldn't be optimal in my case. Because there can be hundreds of workitems per project
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Emre
EmreOP14mo ago
i think i understand what you mean now
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Emre
EmreOP14mo ago
hahah thanks
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server