Note that there are some explanatory texts on larger screens.

plurals
  1. POEF4 Audit changes of many to many relationships
    text
    copied!<p>I am in the process of adding auditing into my EF4 (model first) application. I can get the details about the structural properties on entities that have changes. I can also see when there have been changes on a many to many relationship. I can see the name of the types involved and what happened (add or remove) but what I'd really like is the Id's of the entities that are involved in the relationship change.</p> <p>Here is what I currently have for tracking changes to many to many relationships:</p> <pre><code>var changes = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified); var auditTime = DateTime.Now; foreach (var change in changes) { if (change.Entity != null &amp;&amp; change.Entity.GetType().Equals(typeof(AuditTrail))) { continue; } var detailsBuilder = new StringBuilder(); if (change.Entity == null &amp; (change.State == EntityState.Added | change.State == EntityState.Deleted)) { detailsBuilder.Append("A link between entities "); foreach (var changedMember in change.EntitySet.ElementType.KeyMembers) { detailsBuilder.AppendFormat("{0}", changedMember.Name); if(change.EntitySet.ElementType.KeyMembers.IndexOf(changedMember) &lt; change.EntitySet.ElementType.KeyMembers.Count -2) { detailsBuilder.Append(", "); } else if (change.EntitySet.ElementType.KeyMembers.IndexOf(changedMember) == change.EntitySet.ElementType.KeyMembers.Count - 2) { detailsBuilder.Append(" and "); } } detailsBuilder.AppendFormat(" was {0}.&lt;br /&gt;", change.State); } } </code></pre> <p>How can I get the details (or even the actual entities) involved in the relationship change?</p> <p><strong>UPDATE</strong></p> <p>After poking around on for a few more hours I have managed to find the information I need (see attached image). However, the classes that store the data are internal sealed classes and I can't find a public entry to query the object state manager to get this information out back. So I can audit the change.</p> <p><img src="https://i.stack.imgur.com/TFSxe.png" alt="alt text"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload