Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core

c# entity-framework-core

Question

I'm having problems to update-database my classes into the database.

Does anyone know how to solve it?

Introducing FOREIGN KEY constraint 'FK_Sale_Service_Service_ServiceId' on table 'Sale_Service' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

ENTITIES

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

MAPPINGS

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

PART OF THE SCRIPT

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
3/13/2018 5:17:18 PM

Accepted Answer

I found a solution.

The problem was with the Category, both of them had a foreign key to the same table with a delete cascade, thus, if I deleted a category, the following tables will be affected.

Sale, Service

And most important , SaleService (Join table)

My solution was very simple, I just created a category for each one.

SaleCategory and ServiceCategory.

0
2/25/2018 2:01:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow