title: EFCore deleting referenced entities
body: <p>I have two entities in a project: <code>SupplierFinishingItem</code> and <code>ProductOptionListItem</code>. </p>
  
  <p><code>ProductOptionListItem</code> refrences the other by a navigation property.</p>
  
  <p>When I try to create 1 <code>ProductOptionListItem</code> referencing a <code>SupplierFinishingItem</code> it works and saves the relation in the database.</p>
  
  <p>But when I try to create 2 or more <code>ProductOptionListItem</code> referencing to the same <code>SupplierFinishingItem</code> only the first entity has the relation saved to the database. The others are saved with a <code>null</code> reference. </p>
  
  <p>I managed to reproduce it in the smallest console application I could:</p>
  
  <pre><code>using Microsoft.EntityFrameworkCore;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace relationship_test
  {
      class Program
      {
          static void Main(string[] args)
          {
              using (var db = new DatabaseContext())
              {
                  var finishing = new SupplierFinishingItem { Name = "Finishing"};
                  db.Finishings.Add(finishing);
  
                  db.SaveChanges();
  
                  db.Options.Add(new ProductOptionListItem { Name = "Option 1", SupplierFinishingItem = finishing });
                  db.Options.Add(new ProductOptionListItem { Name = "Option 2", SupplierFinishingItem = finishing });
                  db.Options.Add(new ProductOptionListItem { Name = "Option 3", SupplierFinishingItem = finishing });
  
                  db.SaveChanges();
              }
          }
      }
  
      public class DatabaseContext : DbContext
      {
          public DbSet&lt;ProductOptionListItem&gt; Options { get; set; }
          public DbSet&lt;SupplierFinishingItem&gt; Finishings { get; set; }
  
          protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
          {
              optionsBuilder.UseSqlServer(
                  @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=entity-test;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;ProductOptionListItem&gt;()
                  .HasOne(p =&gt; p.SupplierFinishingItem)
                  .WithMany(s =&gt; s.UsedBy)
                  .HasForeignKey(p =&gt; p.SupplierFinishingItemId)
                  .OnDelete(DeleteBehavior.Restrict);
          }
      }
  
      public class ProductOptionListItem
      {
          public Guid Id { get; set; }
          public string Name { get; set; }
          public Guid? SupplierFinishingItemId { get; set; }
          public SupplierFinishingItem SupplierFinishingItem { get; set; }
      }
  
      public class SupplierFinishingItem
      {
          private HashSet&lt;ProductOptionListItem&gt; _usedBy;
  
          public Guid Id { get; set; }
          public string Name { get; set; }
          public IEnumerable&lt;ProductOptionListItem&gt; UsedBy =&gt; _usedBy?.ToList();
      }
  }
  </code></pre>
  
  <p>The resulting database after I run this code is the following:</p>
  
  <p><a href="https://i.stack.imgur.com/uU8Xc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uU8Xc.png" alt="enter image description here"></a></p>
  
  <p>As you can see, only <code>Option 1</code> has a <code>SupplierFinishingItemId</code>, the other two are <code>NULL</code>.</p>
  
  <p>What am I missing here?</p>