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

ⅰ亾dé卋堺 提交于 2019-12-01 22:50:36

问题


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

public partial class AccountHolder
{
    public int AccountHolderId { get; set; }

    public virtual List<Address> Address { get; set; }
}

public partial class Nominee
{
    public int NomineeId { get; set; }

    public virtual List<Address> Address { get; set; }
}

public partial class Address
{
    public int AddressId { get; set; }

    public int AccountHolderId { get; set; }
    public AccountHolder AccountHolder { get; set; }

    public int NomineeId { get; set; }
    public Nominee Nominee { get; set; }
}

Here AccountHolder and Nominee both have 1 to * replationship with the address. The fluent API code which i used for this is :

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>().HasRequired(p => p.AccountHolder)
                                      .WithMany(p => p.Address)
                                      .HasForeignKey(p => p.AccountHolderId)
                                      .WillCascadeOnDelete(false);
        modelBuilder.Entity<Address>().HasRequired(p => p.Nominee) 
                                      .WithMany(p => p.Address)
                                      .HasForeignKey(p => p.NomineeId)
                                      .WillCascadeOnDelete(false);
    }

Now my issue is whenever i am trying to insert data in AccountHolder or Nominee i got this exception :

In case of AccountHolder insertion

{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Addresses_dbo.Nominees_NomineeId\". The conflict occurred in database \"CodeFirst.BankContext\", table \"dbo.Nominees\", column 'NomineeId'.\r\nThe statement has been terminated."}

In case of Nominee insertion

{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Addresses_dbo.AccountHolders_AccountHolderId\". The conflict occurred in database \"CodeFirst.BankContext\", table \"dbo.AccountHolders\", column 'AccountHolderId'.\r\nThe statement has been terminated."}

Cananybody please tell me how to solve this issue and what i am missing here ?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/15185991/in-code-first-the-insert-statement-conflicted-with-the-foreign-key-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!