C# – How to tell NHibernate to save only changed properties

cdetachnetnhibernate

I have class Person mapped to database with NHibernate.
I load objects from DB and send it to different clients.
First client will modify Name and Country property.
Second client will modify only Name property.
Then both returns modified objects to server.
When I save data from first client – then saved correctly, both – name and country updated.
When I save data from second client – I have problem. It was override data from first client and save new name and initial value of country.

How I can tell NHibernate to save only Name value and not override Country value?

public class Person
{
    public string Name { get; set; }
    public string Country { get; set; }
}

public static List<Person> GetEntities()
{
    var factory = CreateSessionFactory();
    using (ISession session = factory.OpenSession())
    {
        return session.CreateCriteria<Person>().List<Person>();                
    }
}

public static void SaveEntities(List<Person> entities)
{
    var factory = CreateSessionFactory();
    using (ISession session = factory.OpenSession())
    {
         using (var t = session.BeginTransaction())
         {
             foreach (var person in entities)
             {
                 session.Merge(person);
             }

             t.Commit();
        }
    }
}

P.S: Sorry for my bad english

Best Answer

Actually, you can tell NHibernate to specifically update the "dirty" fields using Dynamic Update.

More on: http://ayende.com/blog/3946/nhibernate-mapping-concurrency