1

I crete table in asp.net mvc but when i crete the migration this error message show

Introducing FOREIGN KEY constraint 'FK_dbo.DailyTransactions_dbo.Contracts_ContractId' on table 'DailyTransactions' 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. See previous errors.

this is DailyTransactions table :

 public class DailyTransactions
{
    [Key]
    public int DailyTransactions_Id { get; set; }

    public double Account { get; set; }

    public string Account_Name { get; set; }

    public double Debit { get; set; }

    public double Credit { get; set; }

    public DateTime Date { get; set; }

    public string Remarks { get; set; }

    public int CustomerId { get; set; }

    [ForeignKey("CustomerId")]
    public virtual Customers customers { get; set; }

    public int ContractId { get; set; }

    [ForeignKey("ContractId")]
    public virtual Contracts contracts { get; set; }

}

and this contract table :

 public class Contracts
{
    [Key]
    public int Contracts_Id { get; set; }

    public int Contract_Num { get; set; }

    public DateTime Contract_Start { get; set; }

    public DateTime Contract_End { get; set; }

    public string Status { get; set; }

     public string TypeOfRent { get; set; }

    public double AmountOfRent { get; set; }

     public double Total { get; set; }

    public int CustomerId { get; set; }

      [ForeignKey("CustomerId")]
    public virtual Customers customers { get; set; }

      public int sectionsId { get; set; }

      [ForeignKey("sectionsId")]
      public virtual Sections sections { get; set; }



}
5
  • Try adding WillCascadeOnDelete(false) in modelBuilder.Entity<DailyTransactions>(), because you don't have any [Required] attribute annotations. Commented Feb 27, 2019 at 8:18
  • i don,t understand where i added Commented Feb 27, 2019 at 8:20
  • You need to add it inside OnModelCreating method (I assumed you're using Code First here). Also you may want to try modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); too. Commented Feb 27, 2019 at 8:29
  • 1
    Possible duplicate of Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why? Commented Feb 27, 2019 at 8:34
  • Thank you ,, i solve the error Commented Feb 27, 2019 at 10:08

1 Answer 1

1

Try to turn off CascadeDelete for DailyTransactions and Contracts:

modelBuilder.Entity<DailyTransactions>()
    .HasRequired(c => c.Contracts)
    .WithMany()
    .WillCascadeOnDelete(false);

For example:

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



    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DailyTransactions>()
            .HasRequired(c => c.Contracts)
            .WithMany()
            .WillCascadeOnDelete(false);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

OK i will try .
@SoftwareDeveloper I am glad to help you!:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.