tags:

views:

44

answers:

1

Well, the question kinda nails it down. I'm currently doing something like this:

using (var session = _sessionFactory.OpenSession())
{
     using (var transaction = session.BeginTransaction())
     {
         Car newCar = new Car();
         newCar.name = "Jeep";

         session.Save(newCar);
         transaction.Commit();    
     }
}
+2  A: 
return newCar.Id;

After you've committed your transaction of course.

anthony
Hmmm...that is not being set by the commit according to the debugger. Is there something I need to set for that member variable in the mapping to enable this behavior?
CrypticPrime
@CrypticPrime and you have your breakpoint on the return statement? and primary key is mapped as identity?
dotjoe
This answer is actually correct. The problem is that I had the following in the mapping: Id(x => x.carID) .Column("carID"); .GeneratedBy.Assigned(); // OFFENDING LINERemoving the OFFENDING LINE lets NHibernate populate that variable with the newly generated primary key the database auto-increments.It was legacy code from a prototype and is now removed.
CrypticPrime