I'm having some problems with NHibernate commiting my object changes back to the database. I have an object framework that utilizes NHibernate and the UI interacts with these objects via a WCF service, so I need to operate in a disconnected environment (since I don't want to manage the session on the individual objects). I have no problem retrieving data from the database but I can't seem to persist it back if I disconnect the object from the session and then try to reconnect it later (as a side note, i've tried committing to the database while in the same session and it does work so I know there's no problem with the mapping files etc). Here is the code I use to create the object:
Code:
public static T Get(int id)
{
using (ISessionFactory factory = new Configuration().Configure().BuildSessionFactory())
{
using (ISession session = factory.OpenSession())
{
return (T)session.Load(typeof(T), id, LockMode.Upgrade);
}
}
}
When I attempt to save the object, I use the following:
Code:
public virtual void Save()
{
if (this.ID != null)
this.DateEdited = DateTime.Now;
else
this.DateAdded = DateTime.Now;
using (ISessionFactory factory = new Configuration().Configure().BuildSessionFactory())
{
using (ISession session = factory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Lock(this, LockMode.None);
session.SaveOrUpdate(this);
transaction.Commit();
}
session.Flush();
}
}
}
I don't get any errors when I save and it appears to work fine (no exceptions etc) but no data is actually saved back to the database. Can someone clue me in on what I'm missing? I've been over all the documentation and from what I can tell, I'm doing everything correctly. I've tried experimenting with different lockmodes but none of them seem to do the trick.
Thanks,