PRIMARY KEY violation
See original GitHub issueDescribe your issue
I have a system that creates a lot of “Save” requests to the database. For the procedure, I use the .InsertOrReplace() method, but I get the PK violation a few times an hour. Why is this so? Shouldn’t this method be PK violation safe? Any solutions for this?
Stack trace:
System.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_Orders'. Cannot insert duplicate key in object 'dbo.Orders'. The duplicate key value is (52529786).
The statement has been terminated.
at Commerce.Orders.Infrastructure.Sql.Repositories.Internal.OrderBaseRepository.Save(Order order, Boolean publishDomainEvents)
at Commerce.Orders.Infrastructure.Sql.Repositories.OrderRepository.Save(Order order, Boolean publishDomainEvents)
at Commerce.Orders.Infrastructure.Raven.MigrationRepositories.MigrationOrderRepository.Save(Order order, Boolean publishDomainEvents)
Error Number:2627,State:1,Class:14
db.InsertOrReplace(convertedOrder);
Environment details
Linq To DB
version: 4.2.0
Database (with version): SQL Server 2019
ADO.NET Provider (with version): System.Data.SqlClient 4.8.2
.NET Version: .NET framework 4.8
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Violation of PRIMARY KEY constraint. Cannot insert ...
Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object · 8 · 2 · 1 · what is your unique key...
Read more >Error "Violation of PRIMARY KEY constraint. Cannot insert ...
It happens when the records you are trying to store inside your Target Data Extension contain the same primary key value more than...
Read more >Violation of PRIMARY KEY constraint - Microsoft Q&A
Hi I have query as below at the end which when run gives the 'Violation of PRIMARY KEY constraint 'PK_Staff'. Cannot insert duplicate...
Read more >"Violation of PRIMARY KEY constraint. Cannot insert ...
This error is due to your query returning duplicate values for your primary key column. I assume your primary key column is SubscriberKey....
Read more >How to solve violation of PRIMARY KEY constraint ...
Open up SSMS and navigate to your table in your database. Expand the keys node and you should see the PK_Teacher. Right click...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Serializable should help. Another option could be something like that:
Why does merge API operations cost so much more? InsertOrReplace was done in 1-3 ms and the code you sent does it in 45-60 ms