简体   繁体   English

表'UsageSummaries'上的多态与引入FOREIGN KEY约束可能会导致循环或多个级联路径

[英]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.

相关问题 ef核心2-在表&#39;Y&#39;上引入FOREIGN KEY约束&#39;X&#39;可能会导致循环或多个级联路径 - ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths 在表“模型”上引入FOREIGN KEY约束“列”可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths 在表table上引入FOREIGN KEY约束键可能会导致循环或多个级联路径。 指定ON DELETE…错误 - Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE … Error 引入FOREIGN KEY约束可能会导致EF Core中的循环或多个级联路径 - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core 避免“引入FOREIGN KEY约束可能会导致循环或多个级联路径” - Avoiding 'Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths' 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Error: introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths 引入 FOREIGN KEY 约束...可能导致循环或多个级联路径 - 实体框架 - Introducing FOREIGN KEY constraint ... may cause cycles or multiple cascade paths - Entity Framework 在表 '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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM