❔ EFCore: Duplicate key violates unique constraint
The exception gets triggered at SaveChangesAsync (in the picture).
one ScrapedUser can have many ScrapeTargets (as Parents) one ScrapeTarget can have many ScrapedUsers (children). So there is n:m mapping. Now I am not adding anything to the scrapetargets table directly, I am just adding the scrapetarget that I queried from the database as parent in a scraped user. I am not sure if its trying to automatically somehow add the referenced scrapetarget in the scrapetargets (which would explain the exception as its trying to add an existing one) or whats happening.
26 Replies
What is "target"?
comes from here
Most likely you're trying to "INSERT" a record, that already exists
well the exception says its in the ScrapeTarget table
but im not adding anything there
im just getting targets from there
nothing else
im just adding target to parents of a scrapeduser
parents list
here is the relation
scrapetarget to scrapeduser
hm, best thing you can do right now is debug the changetracker
to see what's trying to add
Change Tracker Debugging - EF Core
Using the ChangeTracker DebugView and log messages to debug EF Core change tracking
set a breakpoint on "savechangesasync" (and dont execute it when you reach there)
fuk
I actually cant really
then look at the debug view
because it needs to run on the server
like its not possible to do it on my local
though it must be that
it tries to add it to ScrapeTargets table
for whatever reason
without me actually adding it
Maybe this line
It's trying to add the target
I'm not completely sure though
yeah however in a completely different table, not the scrapetargets table
since the mapping is n:m
it creates a new table
but maybe you are right
maybe its trying to add it in scrapetargets table before adding the ID in the n:m table
like its probably both
that line and the other
but how would I fix this lmao
I don't exactly know how M2M relationships work in EF-Core. Typically I have the model of the joint table and I do it that way
yeah it works exactly that way. It takes PK of both tables and creates a new one with these two as PK
Yeah but I have a separate model for this joint model.. I think there's also a way to skip it
when I mean model I mean a literal Model.cs
yeah I just have two Lists. Like in Parent I have List of Children and in Children List of Parents
and then I just say its n:m
maybe the issue lies there but its fine according to ms docs
¯\_(ツ)_/¯
you showed how you have "Targets" but what is "Target"
i mean in this screenshot it says target
but here its targets
yeah I loop through all targets, there is where "target" comes from
I can also send code if needed but like the issue is really that line
where I add it as parent to a scrapeduser
so it is trying to add the scrapetarget to scrapetargets table however I never do it in my code its doing it autonatically when trying to add it as parent
this is the issue I face
basically
are you maybe grabbing the target from another context?
no
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.