In Code first The INSERT statement conflicted with the FOREIGN KEY constraint

后端 未结 2 1196
鱼传尺愫
鱼传尺愫 2021-01-21 01:15

I just started using Code first approach for creating databases. I have following 3 tables :

public partial class AccountHolder
{
    public int AccountHolderId          


        
相关标签:
2条回答
  • 2021-01-21 02:09

    In your table dbo.AccountHolders and dbo.Nominees, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table.

    If you have SQL Server Management Studio, open it up and sp_help 'dbo.dbo.Nominees'. See which column that FK is on, and which column of which table it references. You're inserting some bad data.Do the same for sp_help 'dbo.dbo.AccountHolders '.

    Hope this helps.

    0 讨论(0)
  • 2021-01-21 02:10

    You try to insert Address Nominees_NomineeId that is not exists in Nominee.You should First instance of Nominee then fill value and create Address and fill value last create instance of AccountHolder.and then save

    0 讨论(0)
提交回复
热议问题