title: How can I delete orphaned nested entities in the DB after removing them from a List?
body: <p>I am building a backend for an Angular 8 SPA using .Net Core 2.2.
  I am managing <code>Orders</code> - each order has an <code>IList&lt;OrderLine&gt;</code>. 
  I want to use the <a href="https://sourcemaking.com/design_patterns/visitor" rel="nofollow noreferrer">visitor pattern</a> to implement a simple <code>OrdersService</code> that only has one update method and does not need to know about any business rules. </p>
  
  <p>I have an abstract <code>OrderUpdateAction</code>class that holds an <code>Order</code>object. The class has an abstract <code>Do()</code> method (each implementation of this method holds business logic). <code>Do()</code> returns the updated <code>Order</code>object.</p>
  
  <p>My <code>OrdersService</code> has only one <code>UpdateOrder(OrderUpdateAction orderUpdate)</code>method that calls <code>Do()</code>and then hands the updated <code>Order</code> to a DbContext for storing the changes to the Db.</p>
  
  <p><strong>Entities</strong></p>
  
  <pre><code>public class Order 
  {
     public Guid Id {get; set;}
  
     public IList&lt;OrderLine&gt; OrderLines {get; set;}
  
     [more stuff here]
  }
  
  public class OrderLine
  {
     public Guid Id {get; set;}
  
     public Guid? OrderId { get; set; }
  
     public Order Order { get; set; }
  
     public string Description {get; set;}
  
     [more stuff here]
  }
  </code></pre>
  
  <p><strong>OrderUpdateActions</strong></p>
  
  <pre><code>public abstract class OrderUpdateAction
  {
       public Order Order { get; set; }
       public abstract Order Do();
  }
  
  public class DeleteAction : OrderUpdateAction
  {
  
       public Guid OrderLineId { get; set; }
  
       public override Order Do()
       {
  
         if (Order.OrderLines.FirstOrDefault(ol =&gt; ol.Id == OrderLineId) != null)
         {
             Order.OrderLines.Remove(Order.OrderLines.FirstOrDefault(ol =&gt; ol.Id == OrderLineId));
         }
  
         return Order;
       }
  }
  </code></pre>
  
  <p><strong>OrdersService</strong></p>
  
  <pre><code>public async Task&lt;Order&gt; UpdateOrder(OrderUpdateAction orderUpdate)
  {
       var order = orderUpdate.Do();
  
       _dbContext.Orders.Update(order);
  
       await _dbContext.SaveChangesAsync();
  
       [Caching, logging, etc]
  
       return order;
  }
  
  </code></pre>
  
  <p>This all works just fine, but when an <code>OrderLine</code> is removed from the <code>IList&lt;OrderLine&gt;</code> of an <code>Order</code>, the OrderLine is not removed from the database - EF seems to just remove the 'link' between those entities by setting the <code>OrderId</code> in the <code>OrderLine</code> null.</p>
  
  <p>I know that can this behaviour can be useful when multiple 'parent' entities reference that OrderLine, but this is not the case here.</p>
  
  <p>Is there a way to tell EF to delete those orphaned OrderLines, or do I have to manually 'clean up' after the update?</p>
  
  <p>Thanks :)</p>