【发布时间】:2021-05-08 15:07:52
【问题描述】:
我有Affaire,可以有多个Zone
public class Affaire {
// ...
public ICollection<Zone> Zones { get; set; };
}
public class Zone {
// ...
public ICollection<Affaire> Affaires { get; set; };
}
流畅的Zone 配置(仅限Zone):
public override void Configure(EntityTypeBuilder<Zone> builder)
{
// ...
builder
.HasMany(p => p.Affaires)
.WithMany(p => p.Zones)
.UsingEntity(j => j.ToTable("Affaire_Zone"));
}
这是我生成的迁移:
migrationBuilder.CreateTable(
name: "Affaire_Zone",
columns: table => new
{
AffairesId = table.Column<int>(type: "int", nullable: false),
ZonesId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Affaire_Zone", x => new { x.AffairesId, x.ZonesId });
table.ForeignKey(
name: "FK_Affaire_Zone_Affaire_AffairesId",
column: x => x.AffairesId,
principalTable: "Affaire",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Affaire_Zone_Zone_ZonesId",
column: x => x.ZonesId,
principalTable: "Zone",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
add-migration 命令通过 OK。但是当我做update-database 时,我得到了错误
引入 FOREIGN KEY 约束“FK_Affaire_Zone_Zone_ZonesId” 表 'Affaire_Zone' 可能会导致循环或多个级联路径。 指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 外键约束。无法创建约束或索引。看 以前的错误。
【问题讨论】:
标签: c# .net entity-framework entity-framework-core ef-core-5.0