title: One to One and One to Many for the same entity type
body: <p>I am trying to design the following entity relationship</p>
  
  <ul>
  <li>Program </li>
  <li>Assembly</li>
  </ul>
  
  <p>A Program can have multiple assemblies. One of the assemblies will be a main assembly. Each assembly belongs to one program only.</p>
  
  <p>The classes are modeled as follows:</p>
  
  <pre><code>public class Program
  {
      public int Id {get;set;}
      public virtual ProgramAssembly MainAssembly {get;set;}
      public virtual ICollection&lt;ProgramAssembly&gt; Assemblies {get;set;}
  }
  
  public class ProgramAssembly
  {
     public int Id {get;set;}
     public virtual Program Program {get;set;}
     public int ProgramId {get;set;}
  }
  </code></pre>
  
  <p>I am not specifying anything with the FluentApi (however, I have nothing against it).<br>
  With the code above, I get this error:</p>
  
  <blockquote>
    <p>An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.</p>
    
    <p>InnerException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.</p>
  </blockquote>
  
  <p>I tried changing the ProgramAssembly to the following</p>
  
  <pre><code>public class ProgramAssembly
  {
      [ForeignKkey("Program")]
      public int Id {get;set;}
      public virtual Program Program {get;set;}
  }
  </code></pre>
  
  <p>However, then I get this error:</p>
  
  <blockquote>
    <p>ProgramAssembly_Program_Source: : Multiplicity is not valid in Role 'ProgramAssembly_Program_Source' in relationship 'ProgramAssembly_Program'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.</p>
  </blockquote>
  
  <p>I also tried a following fluent API approach</p>
  
  <pre><code>modelBuilder.Entity&lt;ProgramAssembly&gt;()
     .HasRequired(a =&gt; a.Program)
     .WithMany(p =&gt; p.Assemblies);
  
  public class ProgramAssembly
      {        
          public int Id { get; set; }
          public virtual Program Program { get; set; }
          [ForeignKey("Program")] //same error with or without this attribute
          public int ProgramId { get; set; }
     }
  </code></pre>
  
  <p>Then the error is:</p>
  
  <blockquote>
    <p>An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.'
    UpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.</p>
  </blockquote>
  
  <p>How do I allow that relationship without changing the approach to adding 'IsPrimary' attribute on Assembly?  </p>
  
  <p>I saw some similar questions on SO, but they were either about EF core, like <a href="https://stackoverflow.com/questions/49159604/ef-core-one-to-one-relation-on-to-many-relation-using-same-entity">this one</a>, or suggested significant logic change like <a href="https://stackoverflow.com/questions/39247771/entity-framework-code-first-one-to-zero-to-one-and-one-to-many-relationship-to">this one</a>, or even <a href="https://stackoverflow.com/questions/48445047/one-to-one-and-one-to-many-on-the-same-entity-in-entity-framework/48450058#48450058">this one</a> that does not even compile.</p>