简体   繁体   English

实体框架多个复合键,在删除时级联

[英]Entity framework multiple composite keys, cascade on delete

I'm having a bit of a problem with configuring composite keys in Entity framework between 3 tables, code first approach. 我在使用代码优先方法在3个表之间的Entity框架中配置复合键时遇到了一些问题。 I have a base class that has the Id, which all of my classes inherit from. 我有一个具有Id的基类,我的所有类都继承自该类。 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; }
}

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: 当我删除表1或表2类型的元素时,上面的代码工作正常,但是它不允许我从表3中删除元素,但有以下异常:

"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 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);
}

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. 我怀疑我可能没有正确配置模型构建器,但似乎无法弄清楚如何配置它以允许删除Table3类型的元素。 Any help would be appreciated. 任何帮助,将不胜感激。

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). 我需要将所有FK变成PK FK(因为它们不允许为空)。 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. 由于这可能不是最佳解决方案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM