简体   繁体   中英

asp.net mvc (Migration)

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; }



}

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);
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM