实体框架复合键,级联删除 [英] Entity framework multiple composite keys, cascade on delete

查看:177
本文介绍了实体框架复合键,级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Entity框架中配置3个表之间的复合键有一些问题,这是代码第一种方法。我有一个基类,它有所有的类继承自己的Id。第一个表具有第二个表项的集合,而它具有第三个表的集合。从表中删除元素时,需要使用复合键进行级联删除。我也使用聚合根模式。

I'm having a bit of a problem with configuring composite keys in Entity framework between 3 tables, code first approach. I have a base class that has the Id, which all of my classes inherit from. The first table has a collection of the second table items, while it has a collection of the third table. I need composite keys for cascade on delete when removing an element from either table. I am also using the aggregate root pattern.

public abstract class BaseClass    
{
    [Key, Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }
}

public class Table1 : BaseClass
{        
    public virtual ICollection<Table2> Table2Collection { get; set; }
    public string Name { get; set; }
}

public class Table2 : BaseClass
{   
    public Table1 Table1 {get; set;}

    [Key, ForeignKey("Table1"), Column(Order=1)]
    public long Table1ID { get; set; }

    public virtual ICollection<Table3> Table3Collection { get; set; }
    public string Name { get; set; }
}

public class Table3 : BaseClass
{   
    [Key, ForeignKey("Table2Id,Table1Id"), Column(Order = 1)]
    public Table2 Table2 { get; set; }

    public long Table2Id{ get; set; }

    public long Table1Id{ get; set; }
    public string Name { get; set; }
}

上面的代码当我删除了一个类型的元素1或Table2,但不允许我从表3中删除一个元素,给出以下例外:

The code above works fine for when I delete an element of type either Table 1 or Table2, but it won't allow me to delete an element from Table3 giving me the following exception:

关系无法更改,因为一个或多个外键属性不可为空。当对关系进行更改时,相关的外键属性设置为空值,如果外键不支持空值,则必须定义新关系,外键属性必须分配另一个非空值,否则不相关的对象必须被删除。

"The relationship could not be changed because one or more of the foreign-key properties is non-nullable.When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted."

Bellow是我的模型构建器:

Bellow is my model builder:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Table2>()
      .HasRequired(x=>x.Table1)
      .WithMany(x =>x.Table2Collection)
      .WillCascadeOnDelete(true);

    modelBuilder.Entity<Table3>()
      .HasRequired(x=>x.Table2)
      .WithMany(x =>x.Table3Collection)
      .WillCascadeOnDelete(true);
}

我怀疑我可能没有正确配置模型构建器,但我可以似乎无法确定如何配置它以允许删除Table3类型的元素。任何帮助将不胜感激。

I suspect that I may have not configured the model builder properly, but I can't seem to figure out how to configure it to allow for deleting an element of type Table3. Any help would be appreciated.

推荐答案

我把这个答案放在这里,可能会碰到与我一样的问题。我需要将所有的FK放入PK FK(因为它们不允许为空)。这有点烦人,因为如果你有一个更复杂的树,你必须管理的密钥数量会越来越深。

Figured out what I was missing. I'm putting this answer here for anyone that might bump into the same problem as I have. I needed to make all FK into PK FK (since they don't allow null). It's a bit annoying since if you have an even more complex tree, the number of keys you'd have to be manage of would grow the deeper you go.

modelBuilder.Entity<Table3>().HasKey(m => new {m.Id, m.Table2Id, m.Table1Id});

如果有人想知道如何缩短要管理的密钥数量,请留下答案。因为这可能不是最好的解决方案。

If anyone has an idea on how to shorten the number of keys to manage please leave an answer. Since this might not be the best solution.

这篇关于实体框架复合键,级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆