Home > front end >  How to handle nested child collections from Parent in EF6?
How to handle nested child collections from Parent in EF6?

Time:01-11

I have a Parent entity with a Child collection and I want to handle the insertion and deletion of child entities from the Parent. The Child cannot exist without the parent. The model is like this:

public class Parent
{
    public int ID { get; set; }

    public virtual ICollection<Child> Children { get; set; }
}

public class Child
{
    public int ID { get; set; }

    public virtual Parent Parent { get; set; } 
}

With this configuration I can I get the following error when I try to delete child entities from the parent:

System.Data.Entity.Core.UpdateException: A relationship from the 'Parent_Child' AssociationSet is in the 'Deleted' state. Given multiplicity constraints, a corresponding 'Parent_Child_Target' must also in the 'Deleted' state.

To solve it, I have created a identifiying relationship by adding a foreign key called "Parent_ID" in the Child and creating a composite key for the Child. This is the code with fluent API:

modelBuilder.Entity<Parent>()
     .HasMany(o => o.Children)
     .WithRequired()
     .HasForeignKey(oi => oi.Parent_ID);
modelBuilder.Entity<Child>()
     .HasKey(c => new { c.Id, c.Parent_ID })
     .Property(c => c.Id)
     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

Like this everything is working fine and I can add/update/remove items of the Children collection from the Parent. However the problem is that I need to add a new collection of NestedChild entities to my Child class as follows and still manage the whole entity from the Parent:

public class Child
{
    public int ID { get; set; }

    public int Parent_ID { get; set; }

   public virtual ICollection<NestedChild> NestedChildren { get; set; }

}

public class NestedChild
{
    public int ID { get; set; }

    public string otherProperty {get; set; }

}

Any suggestion on how to configure it with fluent API?

Without any kind of configuration this is the error I get when the same exception as above but I have tried to configure the NestedChild entity in the same way as Child, by creating an identifiying relationship with a foreign key with Child, and I get this other error:

System.Data.Entity.ModelConfiguration.ModelValidationException: 'One or more validation errors were detected during model generation: Child_NestedChildren_Source_Child_NestedChildren_Target: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

CodePudding user response:

Continue the pattern down another level. And order the compound keys to follow the hierarchy levels, it matters for performance when retrieving child entities and so you don't need two seperate indexes on each entity.

modelBuilder.Entity<Parent>()
     .HasMany(o => o.Children)
     .WithRequired()
     .HasForeignKey(oi => oi.Parent_ID);
modelBuilder.Entity<Child>()
     .HasKey(c => new { c.Parent_ID, c.ID })
     .Property(c => c.Id)
     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

modelBuilder.Entity<Child>()
     .HasMany(o => o.NestedChildren)
     .WithRequired()
     .HasForeignKey(oi => new {oi.Parent_ID, oi.Child_ID});
modelBuilder.Entity<NestedChild>()
     .HasKey(c => new { c.Parent_ID, c.Child_ID, c.Id })
     .Property(c => c.Id)
     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  • Related