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