© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
58 replies
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.
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

duplicate key value violates unique constraint npgsql entity framework core
C#CC# / help
3y ago
duplicate key error efcore
C#CC# / help
2y ago
✅ UNIQUE constraint failed
C#CC# / help
13mo ago
❔ EFCore: Unique Category Name
C#CC# / help
3y ago