[英]Polymorphism vs Introducing FOREIGN KEY constraint '' on table 'UsageSummaries' may cause cycles or multiple cascade paths
I have a following entity set (code sample is simplified, I left only the key & navigation properties below, and removed all data-holding properties) 我有以下实体集(简化了代码示例,仅在下面保留了键和导航属性,并删除了所有数据保存属性)
public class Program
{
public int Id {get;set;}
public virtual ICollection<ProgramUsageSummary> UsageSummaries { get; set; } = new List<ProgramUsageSummary>();
public virtual ICollection<Function> Functions { get; set; } = new List<Function>();
}
public class Function
{
public int Id {get;set;}
public virtual ICollection<FunctionUsageSummary> UsageSummaries { get; set; } = new List<FunctionUsageSummary>();
}
public class ProgramUsageSummary : UsageSummary
{
public int ProgramId { get; set; }
public Program Program { get; set; }
public virtual ICollection<UsageDetail> UsageDetails { get; set; } = new List<UsageDetail>();
}
public class FunctionUsageSummary : UsageSummary
{
public int FunctionId { get; set; }
public Function Function { get; set; }
public virtual ICollection<UsageDetail> UsageDetails { get; set; } = new List<UsageDetail>();
}
public class UsageDetail
{
public int Id { get; set; }
public virtual UsageSummary UsageSummary { get; set; }
public int UsageSummaryId { get; set; }
}
public abstract class UsageSummary
{
public int Id { get; set;}
}
A Program
can have zero or many ProgramUsageSummaries
. 一个
Program
可以具有零个或多个ProgramUsageSummaries
。
Each of the ProgramUsageSummaries
can have one or many UsageDetails
. 每个
ProgramUsageSummaries
可以具有一个或多个UsageDetails
。
A Program
can also have zero or many Functions
, each of which can have zero or many FunctionUsageSummaries
, and each of those will have one or more UsageDetails
. 一个
Program
还可以具有零个或多个Functions
,每个Functions
可以具有零个或多个FunctionUsageSummaries
,并且每个FunctionUsageSummaries
将具有一个或多个UsageDetails
。
With that setup, I am getting the following error: 使用该设置,我得到以下错误:
Introducing FOREIGN KEY constraint 'FK_dbo.UsageSummaries_dbo.Programs_ProgramId' on table 'UsageSummaries' may cause cycles or multiple cascade paths.
在表'UsageSummaries'上引入FOREIGN KEY约束'FK_dbo.UsageSummaries_dbo.Programs_ProgramId'可能会导致循环或多个级联路径。 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约束
How can I keep the polymorphic design (of UsageSummary
) and get rid of this error? 如何保持(
UsageSummary
)的多态设计并摆脱此错误?
The most straightforward change is to configure the DbContext to not cascade deletes on multiple access paths. 最直接的更改是将DbContext配置为不在多个访问路径上级联删除 。 You will just have handle theis in code, eg to delete the FuntionSummaries in before deleting a Function, etc.
您只需在代码中处理theis,例如在删除Function之前先删除FuntionSummaries,等等。
The other way is to remove the abstract base class UsageSummary from your DbContext. 另一种方法是从DbContext中删除抽象基类UsageSummary。 Then the database mapping for the different UsageSummary subtypes will be to separate and unrelated tables.
然后,不同的UsageSummary子类型的数据库映射将成为独立且不相关的表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.