title: EF Core 2.0 Trouble 'Cascading' Inserts for Related Entities When Updating Principle Entity
body: <p>ASP.NET Core 2 Web application using a REST API.  Currently using sqlite3 for development database.   (Also tried migrating to SQL Server and got same results as below).  </p>
  
  <p>I'm sending an entity to web client, the client makes changes to the entity that involve adding a new related entity and then that updated principle entity gets sent back as json in body of PUT a request.  </p>
  
  <p>I was hoping the new related entity would get created automatically, but this is not  happening.  The simple properties on the principle entity are updated properly, but not reference properties.  I'm not getting any exceptions or anything - it just seems to be ignoring the reference properties.   </p>
  
  <p>Simplified Classes (I removed other properties that shouldn't affect the relationship):</p>
  
  <pre><code>public partial class DashboardItem {
      public int Id { get; set; }
      public int? DataObjectId { get; set; }
      public DataObject DataObject { get; set; }
  
  }
  
  public partial class DataObject {
      public int Id { get; set; }
  }
  </code></pre>
  
  <p>Portion of DbContext Fluent API for associated property:</p>
  
  <pre><code>        modelBuilder.Entity&lt;DashboardItem&gt;(entity =&gt; {
              entity.HasOne(p =&gt; p.DataObject)
              .WithMany()
              .HasForeignKey(p =&gt; p.DataObjectId);
          });
  </code></pre>
  
  <p>Controller Method for PUT:</p>
  
  <pre><code>    [HttpPut("{id}")]
      public async Task&lt;IActionResult&gt; PutDashboardItem([FromRoute] int id, [FromBody] DashboardItem entity)
      {
          if (!ModelState.IsValid)
          {
              return BadRequest(ModelState);
          }
  
          if (id != entity.Id)
          {
              return BadRequest();
          }
  
          _context.Entry(entity).State = EntityState.Modified;
  
          try{
              await _context.SaveChangesAsync();
          }catch (DbUpdateConcurrencyException)
          {
              if (!DashboardItemExists(id)){
                  return NotFound();
              }else {
                  throw;
              }
          }
          return NoContent();
      }
  </code></pre>
  
  <p>The simplified json (without all the other properties) would look like this (I've tried different variations of have the foreign key "DataObjectId" removed from the json, set to null, or set to zero in case that might be interferring.):</p>
  
  <pre><code>{
    Id:1,
    DataObjectId:null,
    DataObject:{
      Id: 0
    }
  }
  </code></pre>
  
  <p>When debugging in the controller action method, the  existing "DashboardItem" principle entity created from the request body has the reference property "DataObject" populated before getting added to the DbContext, but the new DataObject never gets created in the database.  There is only a SQL UPDATE statement issued for DashboardItem and no INSERT for DataObject.</p>
  
  <p>I've also tried making the controller method synchronous instead of async, using DbContext.SaveChanges() instead of .SaveChangesAsync(), since there <a href="https://github.com/aspnet/EntityFrameworkCore/issues/7298" rel="nofollow noreferrer">used to be a problem</a> with that in earlier versions of EF Core related to creating related entities, even though I'm using 2.0 which already has a fix for that. Same result.</p>
  
  <p><a href="https://docs.microsoft.com/en-us/ef/core/saving/related-data" rel="nofollow noreferrer">This EFCore Doc sounds like it should just work out of the box.</a></p>
  
  <p>This has worked for me in a prior project.  What am I missing here?</p>