views:

126

answers:

1

I have an EDM, it includes the entities extension and history. My goal is to use history to keep track of all the changes made to extension entity. For example, if extension with ID 223 has its property 'Name_Display' changed - I want the history entity to record this.

I'm using ASP.NET with VB.NET. Where in my code do I put the hook to say, "update the history entity" and what should that hook look like?

+2  A: 

How about the SavingChanges Event:

public override int SaveChanges(SaveOptions options)
{

    foreach (ObjectStateEntry entry in ObjectStateManager.GetObjectStateEntries(
        EntityState.Added | EntityState.Modified))
    {
        //DO STUFF like create history
    }
    return base.SaveChanges(options);
}

Here is a page that might help: http://msdn.microsoft.com/en-us/library/cc716714.aspx

EDIT: Then I think this may help...

http://msdn.microsoft.com/en-us/library/bb896269.aspx

Disclaimer...I have no experience trying to compare orginal and old values

Change Tracking

Change tracking information for the object graph is stored in ObjectStateEntry objects, which are created by the ObjectContext for each attached object. ObjectStateEntry objects store the following information for the entities:

-The EntityKey that determines the identity of an entity.

-The EntityState for the object

-Information about related objects

-The entity set name

-The CurrentValues and OriginalValues of the entity's properties (objects in an Added state do not have original values)

-The names of the entity's modified properties.

To find if the value of a property has changed between the calls to SaveChanges, query >the collection of changed property names returned by the GetModifiedProperties method...

AGoodDisplayName
Thanks, I'll give that a try. I'm wondering though, at this juncture won't any history logic I add just pull what is the cached save value? I want to get the value before it was changed.
davemackey
This is a great help. Do you happen to know of an article that would show me how to develop the code where you have //DO STUFF like create history?
davemackey
No I dont't but I have done something similar using another ORM. I basically had to create a class (myhistoryitem.cs) to hold all the old and new values that I wanted to record with a reference to the record that I was keeping a history for. I would then save off that historyitem on the the saving event.
AGoodDisplayName