1

I am trying to solve a problem with nesting one->many identifying relationships using composite primary keys to achieve this. I have the following code which works as expected:

public class Parent 
{
     public virtual ICollection<UnnamedLocation> UnnamedLocations { get; set; }
}

public class UnnamedLocation : Location {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UnnamedLocationID { get; set; }
    public int ParentID { get; set; }
    public string Description { get; set; }
}

And the identifying relationship established with the following:

modelBuilder.Entity<UnnamedLocation>()
            .HasKey(m => new {m.UnnamedLocationID, m.ParentID})

The problem is I want to add a collection of coordinates to the 'UnnamedLocation' object:

public virtual ICollection<Coordinate> Coordinates {get; set;}

I would then plan to add the same type of identifying relationship so that EF will properly handle orphaned records:

modelBuilder.Entity<Coordinate>()
            .HasKey(m => new {m.CoordinateID, m.UnnamedLocationID})

The problem is (as I think I understand it) EF now sees the PK of the UnnamedLocation as being the combination of UnnamedLocationID and ParentID and will throw an exception like the following:

The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

Matthew
  • 9,851
  • 4
  • 46
  • 77
  • Someone else somewhere must have done this, or there is a glaring design issue I am missing... – Matthew Sep 18 '15 at 19:06
  • Possible duplicate of [How to define nested Identifying Relationships Entity Framework code first](https://stackoverflow.com/questions/13033934/how-to-define-nested-identifying-relationships-entity-framework-code-first) – Serkan Yilmaz Aug 07 '17 at 08:30

0 Answers0