I'm coding database access methods with C#, Entity Framework and SQL Server.
After my database update, I started to get odd " The INSERT statement conflicted with the FOREIGN KEY constraint " error messages. I notice the error with only one database table A when executing my unit tests in Visual Studio.
The scenario for my unit test is as follows:
As the result, the INSERT statement gives the FOREIGN KEY constraint error message. The INSERT fails with every single FK in it, and the INSERT is successful only after I delete all the FK s from the Table A.
Therefore, I think I might have something wrong with my SQL server, or the with the table A.
Here's some statistics of the Table A
I would be most happy to receive any hints for progressing with this item!
Thanks for your help. I think I found a solution for my problem.
I am using DeZing for Databases tool for my database design, and creation of database creation scripts.
I have drawn a relationship from table A to the other table. The relationship had two keys connected as follows
Table A <--------------------------------> CustomerAddress
PK Guid
FK CustomerAddressGuid <-------> PK Guid
FK CustomerGuid <------------------> FK CustomerGuid
Now the DeZing for Databases tool ordered
FK CustomerGuid <------------------> FK CustomerGuid (FK relations first) and
FK CustomerAddressGuid <-------> PK Guid (PK relations as next)
So, even the values were correct, the INSERT failed, because the FK - FK relation was prior to PK - FK.
Unfortunately the DeZing tool did not resolve this scenario automatically, and the relationship order was not easily visible in the tool.
The solution was to re-arrange the linked attributes order in the tool ( PK attributes first ), and re-create the database creation script.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.