[英]Entity Framework cascade delete - cycles or multiple cascade paths
I have 4 classes 我有四节课
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.