Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to drop down to fluent API to achieve your desired schema (Data annotations wouldn't do it). Precisely you have an <code>Independent One-to-Many Self Reference Association</code> that also has a custom name for the foreign key column (People.Parent). Here is how it supposed to get done with EF Code First:</p> <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity&lt;Person&gt;() .HasOptional(p =&gt; p.Parent) .WithMany(p =&gt; p.Children) .IsIndependent() .Map(m =&gt; m.MapKey(p =&gt; p.Id, "ParentID")); } </code></pre> <p>However, this throws an <code>InvalidOperationException</code> with this message <em>Sequence contains more than one matching element</em>. which sounds to be a CTP5 bug as per the link Steven mentioned in his answer.</p> <p>You can use a workaround until this bug get fixed in the RTM and that is to accept the default name for the FK column which is <code>PersonID</code>. For this you need to change your schema a little bit:</p> <pre><code>CREATE TABLE [dbo].[People] ( Id uniqueidentifier not null primary key rowguidcol, Name nvarchar(50) not null, PersonId uniqueidentifier null ) ALTER TABLE [dbo].[People] ADD CONSTRAINT [ParentOfPerson] FOREIGN KEY (PersonId) REFERENCES People (Id) GO ALTER TABLE [dbo].[People] CHECK CONSTRAINT [ParentOfPerson] GO </code></pre> <p>And then using this fluent API will match your data model to the DB Schema:</p> <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity&lt;Person&gt;() .HasOptional(p =&gt; p.Parent) .WithMany(p =&gt; p.Children) .IsIndependent(); } </code></pre> <h3>Add a new Parent record containing a Child:</h3> <pre><code>using (FamilyContext context = new FamilyContext()) { var pebbles = new Person { Id = Guid.NewGuid(), Name = "Pebbles", }; var fred = new Person { Id = Guid.NewGuid(), Name = "Fred", Children = new List&lt;Person&gt;() { pebbles } }; context.People.Add(fred); context.SaveChanges(); } </code></pre> <h3>Add a new Child record containing a Parent:</h3> <pre><code>using (FamilyContext context = new FamilyContext()) { var fred = new Person { Id = Guid.NewGuid(), Name = "Fred", }; var pebbles = new Person { Id = Guid.NewGuid(), Name = "Pebbles", Parent = fred }; context.People.Add(pebbles); var rowCount = context.SaveChanges(); } </code></pre> <p>Both codes has the same effect and that is adding a new parent (Fred) with a child (Pebbles).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload