Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Dirty Entities in NHibernate using C# and FluentNHibernate

Tags:

c#

nhibernate

I need to get the Objects used in a NHibernate Session, that were modified after loading.

The Session Provides the Property IsDirty indicating weather Objects were modified in the Session or not. I Need a Method that Returns me the Objects causing IsDirty to return true.

If you could provide a bit of C# Code to fulfill this Task I would be very greatful.

like image 929
sebastianmehler Avatar asked Nov 05 '13 12:11

sebastianmehler


1 Answers

Based on the comments of JBL my code to find all "dirty" objects

var dirtyObjects = new List<object>();
var sessionImpl = hsession.GetSessionImplementation();
foreach (NHibernate.Engine.EntityEntry entityEntry in sessionImpl.PersistenceContext.EntityEntries.Values)
{
    var loadedState = entityEntry.LoadedState;
    var o = sessionImpl.PersistenceContext.GetEntity(entityEntry.EntityKey);
    var currentState = entityEntry.Persister.GetPropertyValues(o, sessionImpl.EntityMode);
    if (entityEntry.Persister.FindDirty(currentState, loadedState, o, sessionImpl) != null)
    {
        dirtyObjects.Add(entityEntry);
    }
}
like image 66
Fried Avatar answered Oct 11 '22 13:10

Fried