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.