Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>public class Foo { public int Id { get; set; } public virtual Bar Bar { get; set; } } public class Bar { public int Id {get; set;} public virtual Foo LeftFoo {get;set;} public virtual Foo RightFoo {get;set;} } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity&lt;Bar&gt;() .HasOptiona(b =&gt; b.LeftFoo) .WithOptionalDependent() .Map(k = k.MapKey("LeftFooId")); modelBuilder.Entity&lt;Bar&gt;() .HasOptional(b =&gt; b.RightFoo) .WithOptionalDependent() .Map(k =&gt; k.MapKey("RightFooId")); ... } </code></pre> <p><strong><em>Result</em></strong></p> <p><img src="https://i.stack.imgur.com/2IZRN.png" alt="Table Screenshot"></p> <p><strong><em>Edit for seeding</em></strong></p> <p>I would do the following, didn't test the code, but it should work:</p> <pre><code>var bars = new List&lt;Bar&gt;(); bars.Add(new Bar()); bars.Add(new Bar()); ...//this means as many as you need/want bars.ForEach(b =&gt; context.Bars.AddOrUpdate(b)); context.SaveChanges(); var leftFoos = new List&lt;Foo&gt;(); leftFoos.Add(new Foo()); leftFoos.Add(new Foo()); ...//this means as many as you need/want leftFoos.ForEach(f =&gt; context.Foos.AddOrUpdate(f)); context.SaveChanges(); var rightFoos = new List&lt;Foo&gt;(); rightFoos.Add(new Foo()); rightFoos.Add(new Foo()); ...//this means as many as you need/want rightFoos.Foreach(f =&gt; context.Foos.AddOrUpdate(f)); context.SaveChanges(); int i=0; foreach(var bar in bars) { bar.LeftFoo = leftFoos.ElementAt(i); bar.RightFoo = rightFoos.ElementAt(i); i++; } context.SaveChanges(); </code></pre> <p>For simplicity <code>bars</code>, <code>leftFoos</code> and <code>rightFoos</code> have the same number of elements.</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.
 

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