title: Tracking changes in Entity Framework for many-to-many relationships with behavior
body: <p>I'm currently attempting to use Entity Framework's ChangeTracker for auditing purposes.  I'm overriding the SaveChanges() method in my DbContext and creating logs for entities that have been added, modified, or deleted.  Here is the code for that FWIW:</p>
  
  <pre><code>public override int SaveChanges()
  {
      var validStates = new EntityState[] { EntityState.Added, EntityState.Modified, EntityState.Deleted };
      var entities = ChangeTracker.Entries().Where(x =&gt; x.Entity is BaseEntity &amp;&amp; validStates.Contains(x.State));
      var entriesToAudit = new Dictionary&lt;object, EntityState&gt;();
      foreach (var entity in entities)
      {
          entriesToAudit.Add(entity.Entity, entity.State);
      }
      //Save entries first so the IDs of new records will be populated
      var result = base.SaveChanges();
      createAuditLogs(entriesToAudit, entityRelationshipsToAudit, changeUserId);
      return result;
  }
  </code></pre>
  
  <p>This works great for "normal" entities.  For simple many-to-many relationships, however, I had to extend this implementation to include "Independent Associations" as described in <a href="https://stackoverflow.com/questions/7490509/entity-framework-4-1-many-to-many-relationships-change-tracking#answer-9833201">this</a> fantastic SO answer which accesses changes via the ObjectContext like so:</p>
  
  <pre><code>private static IEnumerable&lt;EntityRelationship&gt; GetRelationships(this DbContext context, EntityState relationshipState, Func&lt;ObjectStateEntry, int, object&gt; getValue)
  {
      context.ChangeTracker.DetectChanges();
      var objectContext = ((IObjectContextAdapter)context).ObjectContext;
  
      return objectContext
              .ObjectStateManager
              .GetObjectStateEntries(relationshipState)
              .Where(e =&gt; e.IsRelationship)
              .Select(
                  e =&gt; new EntityRelationship(
                      e.EntitySet.Name,
                      objectContext.GetObjectByKey((EntityKey)getValue(e, 0)),
                      objectContext.GetObjectByKey((EntityKey)getValue(e, 1))));
  }
  </code></pre>
  
  <p>Once implemented, this also worked great, <strong>but only for many-to-many relationships that use a junction table</strong>.  By this, I'm referring to a situation where the relationship is not represented by a class/entity, but only a database table with two columns - one for each foreign key.</p>
  
  <p>There are certain many-to-many relationships in my data model, however, where the relationship has "behavior" (properties).  In this example, <code>ProgramGroup</code> is the many-to-many relationship which has a <code>Pin</code> property:</p>
  
  <pre><code>public class Program
  {
      public int ProgramId { get; set; }
      public List&lt;ProgramGroup&gt; ProgramGroups { get; set; }
  }
  
  public class Group
  {
      public int GroupId { get; set; }
      public IList&lt;ProgramGroup&gt; ProgramGroups { get; set; }
  }
  
  public class ProgramGroup
  {
      public int ProgramGroupId { get; set; }
      public int ProgramId { get; set; }
      public int GroupId { get; set; }
      public string Pin { get; set; }
  }
  </code></pre>
  
  <p>In this situation, I'm not seeing a change to a <code>ProgramGroup</code> (eg. if the Pin is changed) in either the "normal" DbContext ChangeTracker, nor the ObjectContext relationship method.  As I step through the code, though, I can see that the change is in the ObjectContext's StateEntries, but it's entry has <code>IsRelationship=false</code> which, of course, fails the <code>.Where(e =&gt; e.IsRelationship)</code> condition.</p>
  
  <p>My question is why is a many-to-many relationship with behavior not appearing in the normal DbContext ChangeTracker since it's represented by an actual class/entity and why is it not marked as a relationship in the ObjectContext StateEntries?  Also, what is the best practice for accessing these type of changes?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT:</strong>
  In response to @FrancescCastells's comment that perhaps not explicitly defining a configuration for the <code>ProgramGroup</code> is cause of the problem, I added the following configuration:</p>
  
  <pre><code>public class ProgramGroupConfiguration : EntityTypeConfiguration&lt;ProgramGroup&gt;
  {
      public ProgramGroupConfiguration()
      {
          ToTable("ProgramGroups");
          HasKey(p =&gt; p.ProgramGroupId);
  
          Property(p =&gt; p.ProgramGroupId).IsRequired();
          Property(p =&gt; p.ProgramId).IsRequired();
          Property(p =&gt; p.GroupId).IsRequired();
          Property(p =&gt; p.Pin).HasMaxLength(50).IsRequired();
      }
  </code></pre>
  
  <p>And here are my other configurations:</p>
  
  <pre><code>public class ProgramConfiguration : EntityTypeConfiguration&lt;Program&gt;
  {
      public ProgramConfiguration()
      {
          ToTable("Programs");
          HasKey(p =&gt; p.ProgramId);
          Property(p =&gt; p.ProgramId).IsRequired();
          HasMany(p =&gt; p.ProgramGroups).WithRequired(p =&gt; p.Program).HasForeignKey(p =&gt; p.ProgramId);
      }
  }
  
  public class GroupConfiguration : EntityTypeConfiguration&lt;Group&gt;
  {
      public GroupConfiguration()
      {
          ToTable("Groups");
          HasKey(p =&gt; p.GroupId);
          Property(p =&gt; p.GroupId).IsRequired();
          HasMany(p =&gt; p.ProgramGroups).WithRequired(p =&gt; p.Group).HasForeignKey(p =&gt; p.GroupId);
      }
  </code></pre>
  
  <p>When these are implemented, EF still does not show the modified <code>ProgramGroup</code> in the ChangeTracker.</p>