title: How to add custom handling to relationships (join tables) in SaveChanges in DbContext?
body: <p>I have a custom DbContext that I am using to log any changes (inserts, updates, deletes) made through EF to the database.  It works by overriding the <code>SaveChanges</code> method on <code>DbContext</code>.  It works more or less like this:</p>
  
  <pre><code>public override int SaveChanges()
  {
      foreach (DbEntityEntry entry in this.ChangeTracker.Entries())
      {
         Log(entry);
      }
      return base.SaveChanges();
  }
  </code></pre>
  
  <p>This works reasonably well for changes to entities, for instance, a Client or an Employee.  However, I am having a hard time figuring out how to log changes to relationships between entities.  For instance, there is a ClientEmployee join table that has a ClientId and an EmployeeId.  EF correctly interprets this relationship and put a <code>virtual ICollection</code> of the other table on each table.  How can I find which relationships got added and removed during <code>SaveChanges()</code>?  I need to be able to log them (whether they were added or deleted) even when the only difference is that a row was created or deleted in the join table.</p>
  
  <p>The temporary solution I have been using is to make my join tables into entities by adding a primary key and creating the rows manually in code, but this is cumbersome and just doesn't feel right.  In the example above, Employees would have a <code>virtual ICollection&lt;ClientEmployee&gt;</code> and each <code>ClientEmployee</code> entity would have a single <code>Client</code> and a single <code>Employee</code>.  This gets the job done, but it increases developer overhead and potential for error among all the developers who have to learn and follow this unorthodox pattern.</p>
  
  <p>Is there any way I can get a preview of the changes to relationships (each individual inserted or deleted row of the join table, if possible) even though the changes are not, strictly speaking, to entities?</p>