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 ...

Get Learning NHibernate 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.