title: EF Core Not Updating Foreign Key Items Correctly
body: <p>I am trying to update a <code>user</code> and their corresponding <code>items</code> but I am getting strange behavior.  More specifically, the <code>user</code> is updating, but the <code>items</code> are being added to the <code>items</code> table and the previous <code>items</code> that were already present in the table, have a <code>UserId</code> of <code>null</code>, and the new <code>items</code> have the updated <code>UserId</code>.  </p>
  
  <p>I've tried various things like including <code>UserId</code> and <code>ItemId</code> in my REST call but this results in a <code>System.InvalidOperationException</code> for the <code>items</code>.  However, adding a <code>user</code> and their corresponding <code>items</code> to the tables works fine.</p>
  
  <pre><code>public HttpStatusCode UpdateUsersAndItems(User user)
  {
      if (ModelState.IsValid)
      {
          using (MyContext myContext = _myContext as MyContext)
          {
              User updateUser = myContext?.Users?.Include(i =&gt; i.Items).FirstOrDefault(u =&gt; u != null &amp;&amp; u.UserName == user.UserName);
  
              if(updateUser != null)
              {
                  updateUser.UserName = user.UserName;
                  updateUser.Dob = user.Dob;
                  updateUser.Location = user.Location;
                  updateUser.Items = user.Items;
  
                  myContext?.Users?.Update(updateUser);
  
                  int changes = myContext.SaveChanges();
  
                  if (changes &gt; 0)
                  {
                      return HttpStatusCode.Created;
                  }
              }
          }
      }
  
      return HttpStatusCode.NotModified;
  }
  
  namespace TestWebApplication.Database
  {
      public class User
      {
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int UserId { get; set; }
          public string UserName { get; set; }
          public string Dob { get; set; }
          public string Location { get; set; }
  
          public ICollection&lt;Items&gt; Items { get; set; }
      }
  }
  
  
  namespace TestWebApplication.Database
  {
      public class Items
      {
          [Key]
          [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
          public int ItemId { get; set; }
          public string Item { get; set; }
          public string Category { get; set; }
          public string Type { get; set; }
  
          public int? UserId { get; set; }
  
          [ForeignKey("UserId")]
          public virtual User User { get; set; }
      }
  }
  </code></pre>
  
  <p>Rest call</p>
  
  <pre><code>{
      "username": "xxx",
      "dob": "xxx",
      "location": "xxx",
      "items":[{
          "item": "xxx",
          "category": "xxx",
          "type": "xxx"
          },
          {
          "item": "xxx",
          "category": "xxx",
          "type": "xxx"
          },
          {
          "item": "xxx",
          "category": "xxx",
          "type": "xxx"
          }]
  }
  </code></pre>