title: Entity Framework Entity w/ One-to-Many and One-to-One?
body: <p>The following is an simplified example of a parent/child relationship I'm trying to get working in Entity Framework code-first. The parent has a list of children, and one of them is selected as the default:</p>
  
  <pre><code>public class Parent
  {
      [Key]
      public int Id { get; set; }
      public virtual Child DefaultChild { get; set; }          // One-to-One
      public virtual ICollection&lt;Child&gt; Children { get; set; } // One-to-Many
  }
  
  public class Child
  {
      [Key]
      public int Id { get; set; }
      [Required]
      public virtual Parent Parent { get; set; }
  }
  </code></pre>
  
  <p>When I run entity framework's <code>add-migration</code> command, it is setting the Id for Child as a key, however, instead of making it an <code>IDENTITY</code> it is using a Foreign Key reference to the parent Id.</p>
  
  <p>I tried doing the following on my DbContext:</p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;Parent&gt;().HasOptional(p =&gt; p.DefaultChild).WithRequired(c =&gt; c.Parent);
      modelBuilder.Entity&lt;Parent&gt;().HasMany(p =&gt; p.Children).WithRequired(c =&gt; c.Parent);
      base.OnModelCreating(modelBuilder);
  } 
  </code></pre>
  
  <p>However, then it tosses this error: <code>Schema specified is not valid. Errors: The relationship 'Sample.Presentation.DataAccess.Parent_DefaultChild' was not loaded because the type 'Sample.Presentation.DataAccess.Child' is not available.</code></p>
  
  <p>How are you supposed to handle these relationships?</p>
  
  <p>I know I could add a boolean flag to the Child (e.g. <code>IsDefault</code>) and get rid of the DefaultChild relationship on the parent. However, I'd like to avoid that if possible.</p>
  
  <hr>
  
  <p>Edit: I found this <a href="https://stackoverflow.com/questions/15751798/entity-framework-one-to-many-with-default">info on StackOverflow</a> utilizing <code>WithOptionalPrincipal</code>, however, it creates an additional ParentId field on the child as opposed to creating the ChildId on the parent?! Not quite what I want.</p>
  
  <hr>
  
  <p>Edit: Added <code>DatabaseGenerated</code> as suggested in comments. This is what the migrations output:</p>
  
  <pre><code>CreateTable(
      "dbo.Children",
      c =&gt; new
         {
             Id = c.Int(nullable: false, identity: true),
             Parent_Id = c.Int(nullable: false),
         })
         .PrimaryKey(t =&gt; t.Id)
         .ForeignKey("dbo.Parents", t =&gt; t.Id)  // &lt;== I don't want this!
         .ForeignKey("dbo.Parents", t =&gt; t.Parent_Id, cascadeDelete: true)
         .Index(t =&gt; t.Id)
         .Index(t =&gt; t.Parent_Id);
  
   CreateTable(
      "dbo.Parents",
      c =&gt; new
         {
             Id = c.Int(nullable: false, identity: true)  // &lt;== I expect DefaultChildId after this
         })
         .PrimaryKey(t =&gt; t.Id);  // &lt;== and a foreign key referencing Child.Id
  </code></pre>