EntityFramework Trying to create multiple links to the same table, FK Constraint error

1

I have a table called DeliveryRequest and another table called Operator, table DeliveryRequest is as follows:

public class DeliveryRequest
{
    public int ID { get; set; }
    public DateTime Date { get; set; }
    public string UserID { get; set; }
    public string Waybill { get; set; }
    public string Reference { get; set; }
    public int SupplierID { get; set; }
    public Supplier Supplier { get; set; }
    //[ForeignKey("Operator")]
    public int SenderID { get; set; }
    public Operator Sender { get; set; }
    //[ForeignKey("Operator")]
    public int ReceiverID { get; set; }
    public Operator Receiver { get; set; }
    public string Origin { get; set; }
    public string Destination { get; set; }
    public int ServiceID { get; set; }
    public Service Service { get; set; }
}

And table Operator is as follows:

public class Operator
{
    public int ID { get; set; }
    public string Company { get; set; }
    public int ContactID { get; set; }
    public Contact Contact { get; set; }
    public int AddressID { get; set; }
    public Address Address { get; set; }
}

So the problem is, when I am trying to update my database I get a FK Constraint error as follows:

Introducing FOREIGN KEY constraint 'FK_dbo.DeliveryRequests_dbo.Operators_SenderID' on table 'DeliveryRequests' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.

And the previous error is the same. As follows:

System.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_dbo.DeliveryRequests_dbo.Operators_SenderID' on table 'DeliveryRequests' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Focus on the Sender and Receiver part, I am no expert but the error must be there lol

//[ForeignKey("Operator")]
public int SenderID { get; set; }
public Operator Sender { get; set; }
//[ForeignKey("Operator")]
public int ReceiverID { get; set; }
public Operator Receiver { get; set; }
sql
sql-server
asp.net-mvc
entity-framework
asked on Stack Overflow Mar 13, 2019 by Hancs • edited Mar 13, 2019 by Rai Vu

1 Answer

1

It looks like you are using Code First approach. So try to turn off CascadeDelete for DeliveryRequests:

modelBuilder.Entity<DeliveryRequests>()
    .HasRequired(c => c.Operator )
    .WithMany()
    .WillCascadeOnDelete(false);

For example:

public class YourDBContext: DbContext 
{
    public YourDBContext(): base() 
    {
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DeliveryRequests>()
            .HasRequired(c => c.Operator )
            .WithMany()
            .WillCascadeOnDelete(false);    
    }
}
answered on Stack Overflow Mar 13, 2019 by StepUp

User contributions licensed under CC BY-SA 3.0