title: How to update the "LastModifiedDate" timestamp automatically on parent entity when adding/removing child entities
body: <p><strong>Is there a way to <em>automatically</em> enforce parent entity to be timestamped as having been modified, if any of its dependent child items are added/deleted/modified?</strong> The key word is <em>automatically</em>. I know this can be done by manipulating the DbEntry's EntityState or by manually setting the timestamp field in the parent, but I need this done on a number of parent-child entities in a system, so the desire is to have EF (or a related component) automatically do this somehow.</p>
  <hr />
  <h2>More Background and Examples</h2>
  <p>Let's say we have an <strong>Order</strong> and <strong>Order Items</strong> (1-many). When <strong>order items</strong> are added/removed from an order, the parent <strong>order</strong> itself needs to be updated to store the last modified timestamp.</p>
  <pre class="lang-cs prettyprint-override"><code>public interface IModifiableEntity
  {
      DateTime LastModifiedOn { get; set; }
  }
  
  public class Order : IModifiableEntity
  {
      // some Order fields here...
  
      // timestamp for tracking when the order was changed
      public DateTime LastModifiedOn { get; set; }
  
      // list of order items in a child collection 
      public ICollection&lt;OrderItem&gt; OrderItems { get; set; }
  }
  
  public class OrderItem
  {
      public int OrderId { get; set; }
      // other order item fields...
  }
  </code></pre>
  <p><strong>Somewhere in application logic:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public void AddOrderItem(OrderItem orderItem)
  {
      var order = _myDb.Orders.Single(o =&gt; o.Id == orderItem.OrderId);
      order.OrderItems.Add(orderItem);
      _myDb.SaveChanges();
  }
  </code></pre>
  <p>I already have a pattern in place to detect modified entities and set timestamps automatically via EF's <code>SaveChanges</code>, like this:</p>
  <pre class="lang-cs prettyprint-override"><code>public override int SaveChanges()
  {
      var timestamp = DateTime.Now;
  
      foreach (var modifiableEntity in ChangeTracker.Entries&lt;IModifiableEntity&gt;())
      {
          if (modifiableEntity.State == EntityState.Modified)
          {
              modifiableEntity.Entity.UpdatedOn = timestamp;
          }
      }
  
      return base.SaveChanges();
  }
  </code></pre>
  <p>That works great if any direct fields on an <code>IModifiableEntity</code> are updated. That entity's state will then be marked as <code>Modified</code> by EF, and my custom <code>SaveChanges()</code> above will catch it and set the timestamp field correctly.</p>
  <p>The problem is, if you only interact with a child collection property, the parent entity is not marked as <code>modified</code> by EF. I know I can manually force that via <code>context.Entry(myEntity).State</code> or just by manually setting the <code>LastModifiedOn</code> field when adding child items in application logic, but that wouldn't be done centrally, and is easy to forget.</p>
  <p><strong>I DO NOT want to do this:</strong></p>
  <pre class="lang-cs prettyprint-override"><code>public void AddOrderItem(OrderItem orderItem)
  {
      var order = _myDb.Orders.Single(o =&gt; o.Id == orderItem.OrderId);
      order.OrderItems.Add(orderItem);
  
      // this works but is very manual and EF infrastructure specific
      _myDb.Entry(order).State = EntityState.Modified;
  
      // this also works but is very manual and easy to forget
      order.LastModifiedOn = DateTime.Now;
  
      _myDb.SaveChanges();
  }
  </code></pre>
  <p><strong>Any way I can do this centrally and inform EF that a &quot;root&quot; entity of a parent-child relationship needs to be marked as having been updated?</strong></p>