asp.net-core-2.1 - Entity Framework Core 2.1:OnDelete Cascade - 多个级联路径
问题描述
尝试更新数据库时,我在许多不同的地方收到以下错误。
...可能导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
该错误似乎发生在我在一个对象上有多个一对多关系的地方。
我知道已经发表了其他帖子,但我还没有找到真正帮助我理解这个概念的解释。您能否在以下两个示例中解释我应该如何格式化OnModelCreating
以DbContext
实现所需的功能?
示例 1
课程public class Bin
{
public int Id { get; set; }
public int BarnId { get; set; }
public Barn Barn { get; set; }
public int GatewayId { get; set; }
public Gateway Gateway { get; set; }
}
public class Barn
{
public int Id { get; set; }
public ICollection<Bin> Bins { get; set; }
}
public class Gateway
{
public int Id { get; set; }
public ICollection<Bin> Bins { get; set; }
}
移民
migrationBuilder.CreateTable(
name: "Bins",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
BarnId = table.Column<int>(nullable: false),
GatewayId = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Bins", x => x.Id);
table.ForeignKey(
name: "FK_Bins_Barns_BarnId",
column: x => x.BarnId,
principalTable: "Barns",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_Bins_Gateways_GatewayId",
column: x => x.GatewayId,
principalTable: "Gateways",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
尝试创建“FK_Bins_Gateways_GatewayId”时发生错误
我想要的功能:当 aBarn
被删除时,关联Bins
的也会被删除。当 aGateway
被删除时,关联Bins
的不会被删除
示例 2
课程public class Bin
{
public int Id { get; set; }
public ICollection<BinFeed> BinFeeds { get; set;}
}
public class Feed
{
public int Id { get; set; }
public ICollection<BinFeed> BinFeeds { get; set;}
}
public class BinFeed
{
public int Id { get; set; }
public bool Current { get; set;}
public int BinId { get; set; }
public Bin Bin { get; set; }
public int FeedId { get; set; }
public Feed Feed { get; set; }
}
移民
migrationBuilder.CreateTable(
name: "BinFeeds",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
BinId = table.Column<int>(nullable: false),
FeedId = table.Column<int>(nullable: false),
Current = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BinFeeds", x => x.Id);
table.ForeignKey(
name: "FK_BinFeeds_Bins_BinId",
column: x => x.BinId,
principalTable: "Bins",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_BinFeeds_Feeds_FeedId",
column: x => x.FeedId,
principalTable: "Feeds",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
尝试创建“FK_BinFeeds_Feeds_FeedId”时发生错误
我想要的功能:当Bin
被删除时,关联BinFeeds
被删除。当Feed
被删除时,关联BinFeeds
被删除。
解决方案
我过去有类似的问题。我发现如果您不手动配置OnModelCreating它会自动找到关系并且您没有问题(可能它在选项 2 中做同样的事情)。如果您以后不打算从该关系中的表中删除任何数据,您也可以设置onDelete:ReferentialAction.NoAction 。其他解决方案是使用第三方包的触发器或通过 SQL 手动配置。
推荐阅读
- python - 用python展平numpy数组
- json - 是否可以动态设置 json.Unmarshal 的输出?
- python - 将一个数字相加得到一个数字
- c# - Unity游戏构建:如何将分辨率设置为高于屏幕最大值?
- prestashop - 如何在prestashop表单制造商数据库中实时显示产品
- javascript - 移除 :hover 在移动设备上
- javascript - 如何在 JavaScript 中的对象内插入变量?
- dhall - 如何避免 dhall-format 将自然语言文本转换为代码点?
- mysql - 节点 JavaScript 后端将数据从 MysqlDB 发送到 ESP8266
- c# - 从 Xamarin 事件调用时与 HttpClient 的 C# 异步死锁,为什么?