引入FOREIGN KEY约束可能会导致EF Core中出现循环或多个级联路径

时间:2018-02-24 23:52:32

标签: c# entity-framework-core

我在将数据库更新数据库时遇到问题。

有谁知道如何解决它?

  

介绍FOREIGN KEY约束' FK_Sale_Service_Service_ServiceId'在桌子上' Sale_Service'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

实体

public class Sale
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }  
    public IList<ProductSale> Products { get; set; }
    public IList<SaleService> Services { get; set; }

    public Sale()
    {
        Products = new List<ProductSale>();
        Services = new List<SaleService>();
    }
}

public class Service
{
    public int Id { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public float Duration { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
    public IList<ServiceProfessional> Professionais { get; set; }
    public IList<SaleService> Sales { get; set; }
    public IList<ScheduleService> Schedules { get; set; }

    public Service()
    {
        Professionais = new List<ServiceProfessional>();
        Sales = new List<SaleService>();
        Schedules = new List<ScheduleService>();
    }

}


   public class SaleService
{
    public int SaleId { get; set; }
    public Sale Sale { get; set; }
    public int ServiceId { get; set; }
    public Service Service { get; set; }
}

映射

public class SaleConfiguration : IEntityTypeConfiguration<Sale>
{
    public void Configure(EntityTypeBuilder<Sale> builder)
    {

        builder.ToTable("Sale");

        builder
            .Property(c => c.Id)
            .IsRequired();

        builder
            .Property(c => c.Name)
            .HasMaxLength(40)
            .IsRequired();

        builder
            .Property(c => c.StartDate)
            .IsRequired();

        builder
            .Property(c => c.EndDate)
            .IsRequired();
    }
}



public class ServiceConfiguration : IEntityTypeConfiguration<Service>
{
    public void Configure(EntityTypeBuilder<Service> builder)
    {

        builder.ToTable("Service");

        builder
            .Property(c => c.Id)
            .IsRequired();

        builder
            .Property(c => c.Description)
            .HasMaxLength(40)
            .IsRequired();

        builder
            .Property(c => c.Price)
            .HasColumnType("money")
            .IsRequired();

        builder
            .Property(c => c.Duration)
            .IsRequired();

    }

}

  public class SaleServiceConfiguration:IEntityTypeConfiguration<SaleService>
{
    public void Configure(EntityTypeBuilder<SaleService> builder)
    {
        builder.ToTable("Sale_Service");

        builder.HasKey(c => new {c.ServiceId, c.SaleId});
    }
}

脚本的一部分

migrationBuilder.CreateTable(
    name: "Sale_Service",
    columns: table => new
    {
        ServiceId = table.Column<int>(nullable: false),
        SaleId = table.Column<int>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Sale_Service", x => new { x.ServiceId, x.SaleId });
        table.ForeignKey(
            name: "FK_Sale_Service_Sale_SaleId",
            column: x => x.SaleId,
            principalTable: "Sale",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
        table.ForeignKey(
            name: "FK_Sale_Service_Service_ServiceId",
            column: x => x.ServiceId,
            principalTable: "Service",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    });

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

问题在于类别,它们都有一个具有删除级联的同一个表的外键,因此,如果我删除了一个类别,下面的表将受到影响。

出售的, 服务

最重要的是, SaleService (加入表格)

我的解决方案非常简单,我只是为每个人创建了一个类别。

SaleCategory和ServiceCategory。