Saving entities
As with previous chapter, we will begin by going through a piece of code we had used in Chapter 3, Let's Tell NHibernate About Our Database. If you can remember, we had used the following code to save an instance of the Employee
class in the database:
object id = 0; using (var transaction = Session.BeginTransaction()) { id = Session.Save(new Employee { EmployeeNumber = "5987123", Firstname = "Hillary", Lastname = "Gamble", EmailAddress = "hillary.gamble@corporate.com", DateOfBirth = new DateTime(1980, 4, 23), DateOfJoining = new DateTime(2010, 7, 12), IsAdmin = true, Password = "Password" }); transaction.Commit(); }
We started with call to Session.BeginTransaction
which signaled NHibernate to begin a fresh transaction for us. We then created a new instance of the Employee
class, set required properties on it, and passed it into the Save
method on session. The Save
method on the ISession
interface is one of the persistence APIs offered...