title: EF Core - The MERGE statement conflicted with the FOREIGN KEY constraint
body: <p>I need some help understanding the error I'm getting when I try to update a product.</p>
  
  <p>I have read <a href="https://stackoverflow.com/questions/49179473/the-merge-statement-conflicted-with-the-foreign-key-constraint">this similar question</a>, and tried the accepted answer (placing a <code>_context.SaveChanges()</code> after each table, before the final saving of the complete product), but I still get the same error as described below.</p>
  
  <p>These are the involved models:</p>
  
  <pre><code>public class Product
  {
      public int Id { get; set; }
      // some more properties
      public ICollection&lt;IdentifierForProduct&gt; Identifiers { get; set; }
  }
  
  public class IdentifierForProduct
  {
      public int Id { get; set; }
      public int ProductId { get; set; }
      public int ProductIdentifierId { get; set; }
      public string Value { get; set; } // E.g. "4902505154881"
  
      public ProductIdentifier Identifier { get; set; }
      public Product Product { get; set; }
  }
  
  public class ProductIdentifier
  {
      public int Id { get; set; }
      public string Label { get; set; } // E.g. "EAN"
  
      public ICollection&lt;IdentifierForProduct&gt; ProductIdentifiers { get; set; }
  }
  </code></pre>
  
  <p>Initially, after form post, the <code>Identifiers</code> are set (<code>VMProduct</code> is the product view model):</p>
  
  <pre><code>List&lt;IdentifierForProduct&gt; Identifiers = new List&lt;IdentifierForProduct&gt;();
  if (VMProduct.Identifiers != null)
  {
      for (var i = 0; i &lt; VMProduct.Identifiers.Count; i++)
      {
          Identifiers.Add(new IdentifierForProduct
          {
              ProductId = VMProduct.Id,
              ProductIdentifierId = VMProduct.Identifiers[i].Id,
              Value = VMProduct.Identifiers[i].Value
          });
      }
  }
  </code></pre>
  
  <p>Then the product properties are altered according to the changes made in the form:</p>
  
  <pre><code>Product DbM = await GetProduct(VMProduct.Id);
  // some more properties are set
  DbM.Identifiers = Identifiers;
  _context.Update(DbM);
  await _context.SaveChangesAsync();
  </code></pre>
  
  <p>This exception is thrown on <code>await _context.SaveChangesAsync();</code>:</p>
  
  <blockquote>
    <p>SqlException: The MERGE statement conflicted with the FOREIGN KEY constraint "FK_IdentifiersForProducts_ProductIdentifiers_ProductIdentifierId". The conflict occurred in database "MyStore", table "dbo.ProductIdentifiers", column 'Id'.
    The statement has been terminated.
    System.Data.SqlClient.SqlCommand+&lt;>c.b__108_0(Task result)</p>
    
    <p>DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
    Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch+d__32.MoveNext()</p>
  </blockquote>
  
  <p>This is the <code>GetProduct()</code> method:</p>
  
  <pre><code>public async Task&lt;Product&gt; GetProduct(int Id)
  {
      Product DbM = await _context.Products
          .Include(ic =&gt; ic.InCategories)
              .ThenInclude(pc =&gt; pc.ProductCategory)
          .Include(t =&gt; t.Type)
              .ThenInclude(i =&gt; i.Identifiers) // ProductIdentifiersInTypes
                  .ThenInclude(i =&gt; i.Identifier)
              .Include(t =&gt; t.Type)
                  .ThenInclude(p =&gt; p.Properties) // ProductPropertiesInTypes
                      .ThenInclude(p =&gt; p.Property)
                          .ThenInclude(o =&gt; o.Options)
          .Include(p =&gt; p.ProductPropertyOptions)
          .Where(p =&gt; p.Id == Id)
          .SingleOrDefaultAsync();
      return DbM;
  }
  </code></pre>