title: DbUpdateException with entities that do not expose foreign key properties
body: <p>I have an entity model with <code>User</code> and <code>Person</code> entities, such that each <code>User</code> must be associated with exactly 1 <code>Person</code>, and each <code>Person</code> can be associated zero or 1 <code>User</code>. </p>
  
  <pre><code>User (0..1) &lt;-------&gt; (1) Person
  </code></pre>
  
  <p>The association is mapped fluently. Originally I only had it declared on the <code>Person</code> side:</p>
  
  <pre><code>private class PersonOrm : EntityTypeConfiguration&lt;Person&gt;
  {
      internal PersonOrm()
      {
          ToTable(typeof(Person).Name, DbSchemaName.People);
  
          // has zero or one User
          HasOptional(p =&gt; p.User)
              .WithRequired(d =&gt; d.Person)
              .Map(d =&gt; d.MapKey("PersonId"))
              .WillCascadeOnDelete(false)
          ;
  </code></pre>
  
  <p>Since I encountered this error, I also added the same mapping to the <code>User</code> side:</p>
  
  <pre><code>private class UserOrm : EntityTypeConfiguration&lt;User&gt;
  {
      internal UserOrm()
      {
          ToTable(typeof(User).Name, DbSchemaName.Identity);
  
          // has exactly 1 Person
          HasRequired(p =&gt; p.Person)
              .WithOptional(d =&gt; d.User)
              .Map(d =&gt; d.MapKey("PersonId"))
              .WillCascadeOnDelete(false);
  </code></pre>
  
  <p>There is a scenario in the application where a new <code>User</code> can be created and associated with an existing <code>Person</code>. This is where I am having difficulty at the moment. EF is considering <code>User</code> as the dependent side of the relationship, and is putting a <code>PersonId (FK, int, not null)</code> column on the <code>User</code> table. I don't believe it's possible to use a foreign key property on either entity to help EF manage the association (is it?).</p>
  
  <p>Here is some failing code that tries to handle the scenario:</p>
  
  <pre><code>// find out if Person already exists
  var person = context.People.SingleOrDefault(p =&gt; p.Emails.Any(
      e =&gt; e.Value.Equals(emailAddress, StringComparison.OrdinalIgnoreCase)));
  
  // find out if User already exists
  var user = context.Users.SingleOrDefault(
      u =&gt; u.Name.Equals(emailAddress, StringComparison.OrdinalIgnoreCase));
  
  if (user == null)
  {
      user = new User
      {
          Name = emailAddress,
          IsRegistered = isRegistered,
          Person = person ?? PersonFactory.Create(emailAddress),
          // ignore the PersonFactory.Create, that part works
      };
  
      context.Entry(user).State = EntityState.Added;
      context.SaveChanges();
  }
  </code></pre>
  
  <p>This code works fine when <code>person</code> is null (does not already exist in the db). However when <code>person</code> is not null (already exists in db) and <code>user</code> is null, the code attempts to create a new <code>User</code> and associate it with the existing <code>Person</code>. When invoking <code>SaveChanges()</code>, I get a <code>DbUpdateException</code>:</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>
  </blockquote>
  
  <p>The inner exception is:</p>
  
  <blockquote>
    <p>A relationship from the 'User_Person' AssociationSet is in the
    'Deleted' state. Given multiplicity constraints, a corresponding
    'User_Person_Source' must also in the 'Deleted' state.</p>
  </blockquote>
  
  <p>This doesn't make any sense to me because I'm not trying to delete anything, and checking the <code>EntityState</code> of both <code>User</code> and <code>Person</code> shows that <code>User</code> is in the <code>Added</code> state, whereas <code>Person</code> is in the <code>Unchanged</code> state. I have overridden <code>SaveChanges()</code> to demonstrate:</p>
  
  <pre><code>public override int SaveChanges()
  {
      var userChanges = ChangeTracker.Entries&lt;User&gt;().Single();
      var personChanges = ChangeTracker.Entries&lt;Person&gt;().Single();
  
      if (userChanges.State == EntityState.Deleted)
          throw new InvalidOperationException("wtf?");
  
      if (personChanges.State == EntityState.Deleted)
          throw new InvalidOperationException("wtf?");
  
      return base.SaveChanges();
  }
  </code></pre>
  
  <p>When this code executes, neither <code>InvalidOperationException</code> is thrown. Again, <code>userChanges</code> is in the <code>Added</code> state, and <code>personChanges</code> is in the <code>Unchanged</code> state.</p>
  
  <p>What am I doing wrong?</p>