I have a case in which, I need to change the property value in one of the objects after getting from DB, but after the change (without commiting changes to DB), I need to query again DB and get the previous state before change.
Current behaviour I am experiencing is that I am getting the same value in both states (changed and not changed).
var app = mApplicationRepository.GetByID(id);
app.ApplicationStatus = (int)AppStatus.Applied;
engine.Parameter = app;
var appPreviousState = applicationRepository.GetByID(app.ID);
engine.PreviousState = appPreviousState;
if (appPreviousState.ApplicationStatus != app.ApplicationStatus)//Allways false
{
//call to some method
}
The issue you are running into is the fact that the original object (app) is attached to the current context. When you change it, it changes the entitystate to modified but you then grab it again from the same context causing it to be reloaded. There are a couple ways to deal with this but the easiest would be to detach the object from the context after you get it from the DB. You will have to attach it again when you want to save the changes.
Detach object: http://msdn.microsoft.com/en-us/library/bb738697.aspx
Apply changes from detached object: http://msdn.microsoft.com/en-us/library/bb896248.aspx
Another option would be to have seperate contexts for the original and updated objects.