Entity framework multiple composite keys, cascade on delete

女生的网名这么多〃 提交于 2019-12-12 05:57:32

问题


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

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 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. Any help would be appreciated.


回答1:


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.



来源:https://stackoverflow.com/questions/26670415/entity-framework-multiple-composite-keys-cascade-on-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!