title: Entity Framework: Avoid multiple saves for circular references
body: <p>I'm getting this exception when calling SaveChanges on my EF ObjectContext:</p>
  
  <pre><code>Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints
  </code></pre>
  
  <p>I think the problem is because I have a circular dependency on my DB model.</p>
  
  <p>i.e.</p>
  
  <p>Table Users</p>
  
  <ul>
  <li>Id</li>
  <li>ProfilePictureId</li>
  </ul>
  
  <p>Table Pictures</p>
  
  <ul>
  <li>Id</li>
  <li>UserId</li>
  </ul>
  
  <p>I'm creating a new user and setting the picture</p>
  
  <pre><code>var user = _db.Users.CreateObject();
  var picture = _db.Pictures.CreateObject();
  
  picture.User = user;
  user.ProfilePicture = picture;
  
  _db.SaveChanges();
  </code></pre>
  
  <p>But that throws the exception.</p>
  
  <p>If I add an extra call to SaveChanges() after I set the picture's User It works just fine, I just want to avoid that double trip to the DB.</p>
  
  <p>Any ideas of how to achieve this?</p>
  
  <p>Thanks!</p>