私はデータベースに私のクラスをデータベースに更新するのに問題があります。
誰もそれを解決する方法を知っていますか?
'Sale_Service'テーブルのFOREIGN KEY制約 'FK_Sale_Service_Service_ServiceId'を導入すると、サイクルまたは複数のカスケードパスが発生する可能性があります。 NO DELETE NO ACTIONまたはUP UP NO 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; }
}
地図作成
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);
});
私は解決策を見つけた。
問題はカテゴリであり、両方とも削除カスケードを持つ同じテーブルへの外部キーを持っていました。したがって、カテゴリを削除すると、次のテーブルが影響を受けます。
セール 、 サービス
そして最も重要なのは、 SaleService (Joinテーブル)
私のソリューションは非常にシンプルでしたが、それぞれのカテゴリを作成しました。
SaleCategoryとServiceCategoryです。