title: Unable to determine a valid ordering for dependent operations circular reference
body: <p>I've been wasting two days now to try solve this problem but have yet to find a solution.</p>
  
  <p>In my code that saves an entity with a relationship, I get this error when reaching <code>ctx.SaveChanges()</code>:</p>
  
  <blockquote>
    <p>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><em>Shipment.cs</em></p>
  
  <pre><code>[ForeignKey("ShipmentNumber")]
  public int? DefaultShipmentNumber { get; set; }
  
  public virtual ShipmentNumber ShipmentNumber { get; set; }
  </code></pre>
  
  <p><em>ShipmentNumber.cs</em></p>
  
  <pre><code>[Column("shipment_id")]
  [ForeignKey("Shipment")]
  public byte ShipmentId { get; set; }
  
  public virtual Shipment Shipment { get; set; }
  </code></pre>
  
  <p>To avoid circular references, ShipmentNumber belonging to Shipment is nullable (optional), whereas ShipmentNumber's dependency on Shipment is required.</p>
  
  <p>I first create a Shipment, add it and then attach a ShipmentNumber to it and add it to table as well.</p>
  
  <p>Here's the fluent API code:</p>
  
  <pre><code>modelBuilder.Entity&lt;Shipment&gt;()
      .HasOptional&lt;ShipmentNumber&gt;((shipment) =&gt; shipment.ShipmentNumber)
      .WithMany();
  </code></pre>
  
  <p>Shipment has one "true" ShipmentNumber, but many ShipmentNumbers can link to the same Shipment, hence the <code>WithMany()</code> call (relation without a navigator property). In theory, both relations should always return one entity, but I know EF won't allow me a 1:1 relation here, so I'm using optional.</p>
  
  <p>Here's the actual code:</p>
  
  <pre><code>shipment = tracker.Shipment;
  
  ctx.Shipments.Add(shipment);
  shipment.ShipmentNumber = new ShipmentNumber { Number = tracker.ShipmentNumber };
  
  ctx.ShipmentNumbers.Add(shipment.ShipmentNumber);
  
  ctx.SaveChanges();
  </code></pre>
  
  <p>If someone knows how to make it properly save the entity along with the relation, please do tell. I'm totally stuck at the moment.</p>