C
C#14mo 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
UltraWelfare14mo ago
What is "target"?
Kroks
KroksOP14mo ago
comes from here
No description
UltraWelfare
UltraWelfare14mo ago
Most likely you're trying to "INSERT" a record, that already exists
Kroks
KroksOP14mo 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
KroksOP14mo ago
No description
Kroks
KroksOP14mo ago
here is the relation scrapetarget to scrapeduser
UltraWelfare
UltraWelfare14mo ago
hm, best thing you can do right now is debug the changetracker to see what's trying to add
UltraWelfare
UltraWelfare14mo ago
Change Tracker Debugging - EF Core
Using the ChangeTracker DebugView and log messages to debug EF Core change tracking
UltraWelfare
UltraWelfare14mo ago
set a breakpoint on "savechangesasync" (and dont execute it when you reach there)
Kroks
KroksOP14mo ago
fuk I actually cant really
UltraWelfare
UltraWelfare14mo ago
then look at the debug view
Kroks
KroksOP14mo 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
UltraWelfare14mo ago
Maybe this line
UltraWelfare
UltraWelfare14mo ago
No description
UltraWelfare
UltraWelfare14mo ago
It's trying to add the target I'm not completely sure though
Kroks
KroksOP14mo 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
UltraWelfare14mo 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
KroksOP14mo ago
yeah it works exactly that way. It takes PK of both tables and creates a new one with these two as PK
Kroks
KroksOP14mo ago
No description
UltraWelfare
UltraWelfare14mo 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
KroksOP14mo 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
UltraWelfare14mo ago
you showed how you have "Targets" but what is "Target" i mean in this screenshot it says target but here its targets
Kroks
KroksOP14mo 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
UltraWelfare14mo ago
are you maybe grabbing the target from another context?
Kroks
KroksOP14mo ago
no
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