[英]Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core
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.
在表'Sale_Service'上引入FOREIGN KEY约束'FK_Sale_Service_Service_ServiceId'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
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);
});
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) 最重要的是SaleService (联接表)
My solution was very simple, I just created a category for each one. 我的解决方案非常简单,我只是为每个类别创建了一个类别。
SaleCategory and ServiceCategory. SaleCategory和ServiceCategory。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.