C
C#9mo ago
Kroks

❔ EFCore: Duplicate key violates unique constraint

Exception data:
Severity: ERROR
SqlState: 23505
MessageText: duplicate key value violates unique constraint "PK_ScrapeTargets"
Detail: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
SchemaName: public
TableName: ScrapeTargets
ConstraintName: PK_ScrapeTargets
File: nbtinsert.c
Line: 666
Exception data:
Severity: ERROR
SqlState: 23505
MessageText: duplicate key value violates unique constraint "PK_ScrapeTargets"
Detail: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
SchemaName: public
TableName: ScrapeTargets
ConstraintName: PK_ScrapeTargets
File: nbtinsert.c
Line: 666
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.
No description
26 Replies
UltraWelfare
UltraWelfare9mo ago
What is "target"?
Kroks
Kroks9mo ago
comes from here
No description
UltraWelfare
UltraWelfare9mo ago
Most likely you're trying to "INSERT" a record, that already exists
Kroks
Kroks9mo ago
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
Kroks
Kroks9mo ago
No description
Kroks
Kroks9mo ago
here is the relation scrapetarget to scrapeduser
UltraWelfare
UltraWelfare9mo ago
hm, best thing you can do right now is debug the changetracker to see what's trying to add
UltraWelfare
UltraWelfare9mo ago
Change Tracker Debugging - EF Core
Using the ChangeTracker DebugView and log messages to debug EF Core change tracking
UltraWelfare
UltraWelfare9mo ago
set a breakpoint on "savechangesasync" (and dont execute it when you reach there)
Kroks
Kroks9mo ago
fuk I actually cant really
UltraWelfare
UltraWelfare9mo ago
then look at the debug view
Kroks
Kroks9mo ago
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
UltraWelfare
UltraWelfare9mo ago
Maybe this line
UltraWelfare
UltraWelfare9mo ago
No description
UltraWelfare
UltraWelfare9mo ago
It's trying to add the target I'm not completely sure though
Kroks
Kroks9mo ago
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
UltraWelfare
UltraWelfare9mo ago
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
Kroks
Kroks9mo ago
yeah it works exactly that way. It takes PK of both tables and creates a new one with these two as PK
Kroks
Kroks9mo ago
No description
UltraWelfare
UltraWelfare9mo ago
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
Kroks
Kroks9mo ago
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 ¯\_(ツ)_/¯
UltraWelfare
UltraWelfare9mo ago
you showed how you have "Targets" but what is "Target" i mean in this screenshot it says target but here its targets
Kroks
Kroks9mo ago
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
UltraWelfare
UltraWelfare9mo ago
are you maybe grabbing the target from another context?
Kroks
Kroks9mo ago
no
Accord
Accord9mo 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.