简体   繁体   English

实体框架级联删除-循环或多个级联路径

[英]Entity Framework cascade delete - cycles or multiple cascade paths

I have 4 classes 我有四节课

  • Component - contains info about classes and interfaces 组件 -包含有关类和接口的信息
  • Class , Interface - both contain list of methods 接口 -都包含方法列表
  • Method 方法

Each component contains more classes and interfaces , each class and interface has more methods . 每个组件包含更多的接口 ,每个接口具有更多的方法

If I remove component I need to remove also classes , interfaces and their methods . 如果删除组件 ,则还需要删除接口及其方法 Can you give me a hint how to configure OnModelCreating in order to solve that? 您能给我一个提示如何配置OnModelCreating来解决这个问题吗? It seems problematic having method for both class and interface . 同时为接口使用方法似乎有问题。

I'm getting this error: 我收到此错误:

Introducing FOREIGN KEY constraint 'Interface_Methods' on table 'Methods' may cause cycles or multiple cascade paths. 在表“方法”上引入外键约束“接口方法”可能会导致循环或多个级联路径。 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约束。 Could not create constraint. 无法创建约束。

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {


         modelBuilder.Entity<Component>().
           HasMany(t => t.Classes).
           WithOptional().
           WillCascadeOnDelete();

        modelBuilder.Entity<Component>().
           HasMany(t => t.Interfaces).
           WithOptional().
           WillCascadeOnDelete();


        modelBuilder.Entity<Class>().
           HasMany(t => t.Methods).
           WithOptional().
           WillCascadeOnDelete();

       modelBuilder.Entity<Interface>().
          HasMany(t => t.Methods).
          WithOptional().
          WillCascadeOnDelete();


        modelBuilder.Conventions.Remove<ForeignKeyAssociationMultiplicityConvention>();

        base.OnModelCreating(modelBuilder);
    }

Component: 零件:

public class Component
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual List<Class> Classes { get; set; }
    public virtual List<Interface> Interfaces { get; set; }

    public Component()
    {
        Classes = new List<Class>();
        Interfaces = new List<Interface>();
    }
}

Class: 类:

public class Class
{
    [Key]  
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<Method> Methods { get; set; }

    public Class()
    {
        Methods = new List<Method>();
    }
}

Interface: 接口:

public class Class
{
    [Key]  
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<Method> Methods { get; set; }

    public Class()
    {
        Methods = new List<Method>();
    }
}

Method: 方法:

public class Method
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
}

Since a method can be referenced by any number of classes and interfaces, I don't think you would want it to be deleted if one of those classes or interfaces was deleted. 由于一个方法可以被任意数量的类和接口引用,所以我不认为如果删除了其中的一个类或接口,则不希望将其删除。 Instead, consider having a the Component own the methods and delete them. 而是考虑让Component拥有方法并删除它们。

I have not used EF code first so the syntax of this code may not be correct, but hopefully gets the point across: 我没有先使用EF代码,因此该代码的语法可能不正确,但希望可以理解这一点:

modelBuilder.Entity<Component>().
    HasMany(t => t.Classes).
    WithOptional().
    WillCascadeOnDelete();

modelBuilder.Entity<Component>().
    HasMany(t => t.Interfaces).
    WithOptional().
    WillCascadeOnDelete();

modelBuilder.Entity<Component>().
    HasMany(t => t.Methods).
    WithOptional().
    WillCascadeOnDelete();

modelBuilder.Entity<Class>().
    HasMany(t => t.Methods).
    WithOptional();

modelBuilder.Entity<Interface>().
    HasMany(t => t.Methods).
    WithOptional();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首先使用实体​​框架代码:循环或多个级联路径 - Entity Framework Code first: cycles or multiple cascade paths Entity Framework Core:可能导致循环或多个级联路径 - Entity Framework Core: may cause cycles or multiple cascade paths 循环或多级联路径 - Cycles or multiple cascade paths 如何在有周期和多个级联路径的地方配置级联删除 - How to configure cascade delete where there are cycles and multiple cascade paths 避免循环或多级联路径 - Avoid cycles or multiple cascade paths MVC 4实体框架同一表的两个外键导致循环或多个级联路径 - MVC 4 Entity Framework Two Foreign Keys to the Same Table Cause cycles or multiple cascade paths 实体框架C#在表Y中引入外键X可能会导致循环或多个级联路径 - Entity Framework C# Introducing A Foreign Key X In Table Y May Cause Cycles OR Multiple Cascade Paths SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 实体框架,外键约束可能会导致循环或多个级联路径 - Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM