There's a "bug" when using NHibernate with SQL CE identity columns. There are two workarounds that I know of:
1 - Set the connection.release_mode
property to on_close
in configuration:
mySessionFactory = Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard
.ConnectionString("Data Source=MyDB.sdf"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(BuildSchema)
.ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close"))
.BuildSessionFactory();
2 - Perform inserts in a transaction:
using (ISession session = DB.OpenSession())
using (ITransaction txn = session.BeginTransaction())
{
session.Save(employee);
txn.Commit();
}
I realize the question is more than a month old but I hope this answer is still of use to you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…