21 Replies
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
Why are you trying to update an entity like this in the first place at all?
Where did you learn that from?
I usually work with TSQL so i didnt really learn it really
can you tell me what im doing wrong
@Brainiac V
So why not reading through examples or tutorials?
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
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
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
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
I added the no tracking in order to bypass my error. Which also didn't work
So you can also remove this code:
Also don't use SaveChangesAsync as long as you really don't know what the consequences are
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
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•14mo ago
Message Not Public
Sign In & Join Server To View
Then none of the problems above should happen
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
that wouldn't be optimal in my case. Because there can be hundreds of workitems per project
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
i think i understand what you mean now
Unknown User•14mo ago
Message Not Public
Sign In & Join Server To View
hahah thanks
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.