0

I'm currently using DbContext with Ef 4.1, and I'm trying to audit all changes some of my entities. I can capture the original and current values for any Properties of an entity, however I cannot figure out how to capture the association (Foreign Key) OriginalValues of a NavigationProperty. Has anyone figured this out?

mservidio
  • 12,817
  • 9
  • 58
  • 84

1 Answers1

2

You must either include foreign keys to your entities so they will be tracked as normal values or you must convert your DbContext to ObjectContext and use more powerful (and more cumbersome) ObjectStateManager where you can get instances of ObjectStateEntry for both entities and relations.

To convert DbContext to ObjectContext use:

var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;

To get entries use:

var entires = objectContext.ObjectStateManager
                           .GetObjectStateEntries(~EntityState.Unchanged);

Iterate through entries and use their State, CurrentValues and OriginalValues properties to do your logging. Relationship should not be in modified so you should only need to check for deleted and added relationships (instead of updating the old is deleted and new is added). The problem is with deleted once because they will not provide you their values. You can try small workaround by changing their state, get values and changing state back to deleted - if it doesn't work you will not be able to log old values for relations.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • In my initial query I already Include these fk's/related entities, are you saying I should be able to capture the changed and original values when I do this? – mservidio Aug 09 '11 at 22:15
  • Also, I'm not sure if I clarified the question, basically I have 4 columns which fk to static tables. For example, on the table I want to audit changes on, let's say I have a column named transactionid. When the transaction I'd changed I'm looking to access it via the currentvalues and originalvalues. However, as transactionid actually fk's to another table, there is no property on my model, just a navigation property. However, all I want to capture is the integer of the transactionid before and after the change to the object. – mservidio Aug 09 '11 at 22:19
  • They shoul be there for one-to-many and one-to-one relations. – Ladislav Mrnka Aug 09 '11 at 22:19
  • Just found this, perhaps I need these 'FK Associations' so the ID's on my FK's/NavigationProperties are properties within my entities. http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx – mservidio Aug 09 '11 at 22:28
  • FK associations are what I ment. http://stackoverflow.com/questions/5281974/code-first-independent-associations-vs-foreign-key-associations/5282275#5282275 – Ladislav Mrnka Aug 09 '11 at 22:45
  • Thanks! That does it. Now I can access the fk Id's as properties. One other side effect it seems is that I can't create zero to one relations in the same table. Ie: I have a table with Id, and ParentId. Previously I was able to make a relation of Zero to One and Zero to One on both sides of the associatation, as an object in my table should only have at most 1 parent, and at most 1 child, however I can't map this anymore, it throws validation errors. Before implementing the FK property columns I was able to do this. – mservidio Aug 10 '11 at 14:38