AssertionFailure:“空标识符” - FluentNH + SQL服务器CE
代码失败于
session.Save(employee);
with AssertionFailure "null identifier". What am I doing wrong?using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace FNHTest
{
public class Employee
{
public virtual int Id
{
get;
private set;
}
public virtual string Name
{
get;
set;
}
public virtual string Surname
{
get;
set;
}
}
public class EmployeeMap : ClassMap
{
public EmployeeMap()
{
Id(e => e.Id);
Map(e => e.Name);
Map(e => e.Surname);
}
}
public class DB
{
private static ISessionFactory mySessionFactory = null;
private static ISessionFactory SessionFactory
{
get
{
if (mySessionFactory == null)
{
mySessionFactory = Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard
.ConnectionString("Data Source=MyDB.sdf"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
return mySessionFactory;
}
}
private static void BuildSchema(Configuration configuration)
{
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.Execute(false, true, false);
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
public class Program
{
public static void Main(string[] args)
{
var employee = new Employee
{
Name = "John",
Surname = "Smith"
};
using (ISession session = DB.OpenSession())
{
session.Save(employee);
}
}
}
}
The code fails at
session.Save(employee);
with AssertionFailure "null identifier". What am I doing wrong?
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
namespace FNHTest
{
public class Employee
{
public virtual int Id
{
get;
private set;
}
public virtual string Name
{
get;
set;
}
public virtual string Surname
{
get;
set;
}
}
public class EmployeeMap : ClassMap
{
public EmployeeMap()
{
Id(e => e.Id);
Map(e => e.Name);
Map(e => e.Surname);
}
}
public class DB
{
private static ISessionFactory mySessionFactory = null;
private static ISessionFactory SessionFactory
{
get
{
if (mySessionFactory == null)
{
mySessionFactory = Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard
.ConnectionString("Data Source=MyDB.sdf"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
return mySessionFactory;
}
}
private static void BuildSchema(Configuration configuration)
{
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.Execute(false, true, false);
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
public class Program
{
public static void Main(string[] args)
{
var employee = new Employee
{
Name = "John",
Surname = "Smith"
};
using (ISession session = DB.OpenSession())
{
session.Save(employee);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 NHibernate 与 SQL CE 标识列一起使用时存在一个“错误”。我知道有两种解决方法:
1 - 在配置中将
connection.release_mode
属性设置为on_close
:2 - 在事务中执行插入:
我意识到问题更多不到一个月了,但我希望这个答案对您仍然有用。
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 toon_close
in configuration:2 - Perform inserts in a transaction:
I realize the question is more than a month old but I hope this answer is still of use to you.
仍然是真实的。即使第一次调用 SELECT @@IDENTITY 返回 corect 值,但秒返回 null
我使用
*connection.driver_class = NHibernate.Driver.SqlServerCeDriver
方言:NHibernate.Dialect.MsSqlCeDialect*
添加属性后
*connection.release_mode: on_close*
我的 NHIbernate 配置可以正常工作
Is is still the true. Even first call to SELECT @@IDENTITY returns corect value, but seconds returns null
I use
*connection.driver_class = NHibernate.Driver.SqlServerCeDriver
dialect: NHibernate.Dialect.MsSqlCeDialect*
After adding property
*connection.release_mode: on_close*
to my NHIbernate configuration it works