在表"XY"上引入FOREIGN KEY约束"FK_XY"可能会导致循环或多个级联路径.指定ON DELETE NO ACTION [英] Introducing FOREIGN KEY constraint 'FK_XY' on table 'XY' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION
问题描述
我有 Affaire
,可以有多个 Zone
public class Affaire {
// ...
public ICollection<Zone> Zones { get; set; };
}
public class Zone {
// ...
public ICollection<Affaire> Affaires { get; set; };
}
流畅的 Zone
配置(仅在 Zone
上):
the fluent Zone
configuration (only on 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
时,我得到了错误
The add-migration
command passes OK. but when I do update-database
I get the error
在上引入FOREIGN KEY约束'FK_Affaire_Zone_Zone_ZonesId'表"Affaire_Zone"可能会导致循环或多个级联路径.指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他外键约束.无法创建约束或索引.看以前的错误.
Introducing FOREIGN KEY constraint 'FK_Affaire_Zone_Zone_ZonesId' on table 'Affaire_Zone' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.
推荐答案
通过手动更新迁移代码而部分解决,但是当我再次进行迁移时,它将删除此代码
Solved partially by manually updating the Migration code, but when I will do another migration it will remove this code
更改了
onDelete: ReferentialAction.Cascade);
到
onDelete: ReferentialAction.NoAction);
此处:
migrationBuilder.CreateTable(
name: "AffaireZone",
columns: table => new
{
AffairesId = table.Column<int>(type: "int", nullable: false),
ZonesId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AffaireZone", x => new { x.AffairesId, x.ZonesId });
table.ForeignKey(
name: "FK_AffaireZone_Affaire_AffairesId",
column: x => x.AffairesId,
principalTable: "Affaire",
principalColumn: "Id",
onDelete: ReferentialAction.NoAction);
table.ForeignKey(
name: "FK_AffaireZone_Zone_ZonesId",
column: x => x.ZonesId,
principalTable: "Zone",
principalColumn: "Id",
onDelete: ReferentialAction.NoAction);
});
这篇关于在表"XY"上引入FOREIGN KEY约束"FK_XY"可能会导致循环或多个级联路径.指定ON DELETE NO ACTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!