2
votes

I am using NHibernate.JetDriver to persist my entities in an Microsoft Access database. I have a simple table named Employee with 3 columns:

  • Id (auto generated number)
  • FirstName (text)
  • LastName (text)

Here is the corresponding entity:

class Employee
{
    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

And the Mapping (using Fluent NHibernate):

class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        Id(x => x.Id)
            .GeneratedBy.Native();
        Map(x => x.FirstName);
        Map(x => x.LastName);
    }
}

Then I have the following code wich configure the SessionFactory (using Fluent NHibernate), create a new Employee and save it to the database:

var sessionFactory = Fluently.Configure()
    .Database(JetDriverConfiguration.Standard
        .ConnectionString(@"Provider=Microsoft.ACE.OLEDB.12.0;
            Data Source=.\Company.accdb")
        .ShowSql)
    .Mappings(m => m.FluentMappings
        .AddFromAssemblyOf<EmployeeMap>())
    .BuildSessionFactory();

var employee = new Employee
{
    FirstName = "Laurent",
    LastName = "De Cant"
};

using (var session = sessionFactory.OpenSession())
{
    using (var transaction = session.BeginTransaction())
    {
        var id = session.Save(employee);
        transaction.Commit();
    }
}

My problem is the following: the employee does not seem to be persisted in the database. I have no exception and the id returned by Save is correct. Everything seems to be alright except that when I check the data in the table, There is no employee...

Using ShowSql option, here is the generated SQL:

NHibernate: INSERT INTO `Employee` (FirstName, LastName) VALUES (?, ?);
@p0 = 'Laurent' [Type: String (7)], @p1 = 'De Cant' [Type: String (7)]
NHibernate: select @@identity

Here is what NHibernate profiler gives me:

-- statement #1
begin transaction with isolation level: Unspecified

-- statement #2
INSERT INTO `Employee`
            (FirstName,
             LastName)
VALUES      (?,
             ?)

-- statement #3
select @@identity

-- statement #4
commit transaction
2

2 Answers

2
votes

You're probably looking at the wrong database. During the build process the database file is copied to the output folder. If you check the database in the output folder the Employee record will be present.

1
votes

If you download a trial of NHProf you should see if a session is being opened, the transaction being started, the SQL that is being sent and whether the session is throwing an error or not. If it is throwing an error it may be getting swallowed somewhere. NHProf is database independent so it will work with the Jet drivers.

Mind you you are getting the auto number back so I am not sure but I am sure NHProf will help you see what is going on. I wonder if it is because your birthdate column is a string and your property on the entity is a dateTime type?

Usually if no row is being persisted then this indicates that the entity Employee is not being added to the session factory, however in your case it clearly shows that it is... Please note that NHibernate does not throw exceptions if you try to persist an unmapped entity.