I guess what I'm after here is a quick example of how to do this. I've got fluent nhibernate working fine. I can create new records with no problem, and read in existing records no problem. The issue I'm having is with the asp.net user session and how I handle the fact that I want to keep that around between page loads, and I want to write it to the database occasionally too.
The flow I'm trying to get working is something like this:
- User logs in
- NHibernate fetches user record
- user record is stuffed in asp.net session
- NHibernate session (I think) ends
- app displays a link to "add address" page
- user goes to "add address" page
- user enters address details
- user clicks "submit"
- address is added to user object
- user object is written to database
When I attempt to write the user object (complete with new address) back to the database, I get an error telling me that user object is already associated with an existing session. Below is the code I'm using to get the user out of the asp.net session, add the address and write it back to the database. It's only at the point where it hits .SaveOrUpdate that things go wrong, the address is added to the object perfectly.
void AddAddress_Click(object sender, EventArgs e)
{
var session = Global.SessionFactory.GetCurrentSession();
User user=(User)HttpContext.Current.Session["User"];
user.AddAddress(txtDescription.Text, txtLine1.Text,
txtLine2.Text, txtTown.Text, txtCounty.Text,
txtPostCode.Text, txtCountry.Text);
session.SaveOrUpdate(user);
session.Flush();
}
Please ignore the 101 ways in which this is probably wrong for now, I'm well aware of it. I just want to get something working to prove to myself that NHibernate is the way forward for this project, so it will be a little quick and dirty at the moment.