I have a simple 1:many aggregate relationship, lets say:
public class Parent
{
public string Name {get; set;}
public Child SelectedChild {get; set;}
public Child PublishedChild {get; set;}
public virtual ICollection<Child> AllChildren {get; set;}
}
public class Child
{
public string Name {get; set;}
[Required]
public Parent Father {get; set;}
}
When creating the schema from this model I get the error:
Introducing FOREIGN KEY constraint ‘Parent_SelectedChild’ on table ‘Parent’ may cause cycles or multiple cascade paths.
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints
So I add the following to OnModelCreating:
modelBuilder.Entity<Child>()
.HasRequired(v => v.Parent)
.WithOptional(c => c.SelectedChild)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Child>()
.HasRequired(v => v.Parent)
.WithOptional(c => c.PublishedChild)
.WillCascadeOnDelete(false);
This gets round the original error but I now get:
Unable to determine the principal end of the ‘xxx.Parent_SelectedChild’ relationship.
Multiple added entities may have the same primary key.
Can anyone help?
All I essentially want to do is refer to particular child records on a 1:many aggregate relationship from the parent. I assume EF will create INT child id columns on the parent called e.g. SelectedChild_Id & PublishedChild_Id (or similar).
Thanks in advance
-macon
Edit: In response to @Slauma:
I can get a schema generated using:
modelBuilder.Entity<Parent>()
.HasOptional(p => p.SelectedChild)
.WithOptionalPrincipal()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Parent>()
.HasOptional(p => p.PublishedChild)
.WithOptionalPrincipal()
.WillCascadeOnDelete(false);
modelBuilder.Entity<Parent>()
.HasMany(p => p.AllChildren)
.WithRequired(c => c.Father)
.WillCascadeOnDelete(false);
But this generates multiple FK on the Child record e.g. Parent_Id, Parent_Id1. I just want a reference from the Parent to one of the child rows e.g. Parent_SelectedChildId. Do I have to do this manually with an int column on parent?
I think you have three 1-to-many relationships:
Edit
I’ve tested my mapping above with exactly the
ParentandChildclass you provided in your question – with the only exception that I have added a primary key property to both classes:public int Id { get; set; }. Otherwise EF would complain about a missing key property. This mapping doesn’t throw an exception and creates the following tables in the database:Parents table:
Children table:
So, there are the three foreign key columns as expected.
Since you get an exception according to your comment, I guess that there is actually some important difference in the code you have tested.
BTW: Mapping the two navigation properties of the
Parentclass as One-to-One relationships is much more difficult, if not impossible. In EF you need a shared primary key between the two tables to map a One-to-One relationship, so it would not be possible to assign two different entities to the two navigation properties because they cannot both have the same key as the parent.