asp.net mvc(迁移)

问题描述 投票:1回答:1

我在asp.net mvc中创建了表,但是当我克隆迁移时,此错误消息显示

在表'DailyTransactions'上引入FOREIGN KEY约束'FK_dbo.DailyTransactions_dbo.Contracts_ContractId'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。无法创建约束。查看以前的错误。

这是DailyTransactions表:

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

}

和这份合同表:

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



}
c# sql-server entity-framework ef-code-first database-migration
1个回答
1
投票

尝试为CascadeDeleteDailyTransactions关闭Contracts

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

例如:

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



    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DailyTransactions>()
            .HasRequired(c => c.Contracts)
            .WithMany()
            .WillCascadeOnDelete(false);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.