title: Entity Framework Code First Auditing many-to-many and one-to-many issues
body: <p>I've been trying to create an audit trail attempting many different means of doing so from TrackChanges, override of <code>SaveChanges()</code> and simply grabbing the entity and getting the original values using <code>DbEntityEntry.OriginalValues</code> all of these methods have ultimately boiled down to the same problem. When I have referenced another model as an <code>ICollection</code> it does not record this model in the entity.
  As a very basic explanation of this issue see the code below:</p>
  
  <pre><code>public void Update(Project project)
  {
      _ctx.OriginalValues(project);
      _ctx.Projects.Attach(project);
      _ctx.Entry(project).State = EntityState.Modified;
      _ctx.SaveChanges(project);
  }
  </code></pre>
  
  <p>The <code>.OriginalValues</code> above passes the entity to this method before the save changes has been evoked and I am able to read the values in the entity prior to the user changes. The <code>OriginalValues</code> method looks like so:</p>
  
  <pre><code>public void OriginalValues(object entity)
  {
      var entry = this.Entry(entity);               
      System.Reflection.PropertyInfo[] names = entity.GetType().GetProperties();            
  
      foreach (var property in names)
      {               
          var n = entry.OriginalValues[property.Name];                   
      }
  }
  </code></pre>
  
  <p>The method above I understand is not very elegant or the best practice but it is the latest attempt in a long stream of attempts to read the original values from my entity.</p>
  
  <p>When this code runs the <code>property.Name</code> will get to a name called 'Teams' which is referenced as such in the Project model:</p>
  
  <pre><code>public class Project
  {       
      public int Id { get; set; } // PK
      public string Name { get; set; }
      public DateTime StartDate { get; set; }
      public DateTime EndDate { get; set; }
      public int MarketId { get; set; } // FK
      [ForeignKey("MarketId")]
      public Market Market { get; set; } // FK Nav   
      public ICollection&lt;Team&gt; Teams { get; set; } // Many to Many Nav 
  }
  </code></pre>
  
  <p>An error is thrown which tells me it has not been mapped to Project even though I have used fluent API and data annotations both to try and get a mapping for this.</p>
  
  <p>This is one example I have the same situation occurring in other models but with many to many relationships.</p>
  
  <p>I am a graduate developer and as such don't have a large amount of exposure to .net I hope someone can offer any advice or support on this if possible!</p>
  
  <p>Thanks</p>
  
  <p>Edit: Error message displayed:</p>
  
  <blockquote>
    <p>The 'Teams' property does not exist or is not mapped for the type 'Project'.</p>
    
    <p>Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. </p>
    
    <p>Exception Details: System.ArgumentException: The 'Teams' property does not exist or is not mapped for the type 'Project'.</p>
    
    <p>Source Error: </p>
  </blockquote>
  
  <pre><code>Line 78:             foreach (var property in names)
  Line 79:             {               
  Line 80:                 var n = entry.OriginalValues[property.Name];                   
  Line 81:                 
  Line 82:             }
  </code></pre>