AssertionFailure:“空标识符” - FluentNH + SQLServerCE [英] AssertionFailure: "null identifier" - FluentNH + SQLServerCE

查看:200
本文介绍了AssertionFailure:“空标识符” - FluentNH + SQLServerCE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码在

  session.Save(employee);  

上失败,并带有AssertionFailurenull identifier。我在做什么错?

 使用FluentNHibernate.Cfg; 
使用FluentNHibernate.Cfg.Db;
使用FluentNHibernate.Mapping;
使用NHibernate;
使用NHibernate.Cfg;
使用NHibernate.Tool.hbm2ddl;

namespace FNHTest
{
public class Employee
{
public virtual int Id
{
get;
私人设置;
}

公共虚拟字符串名称
{
get;
set;
}

公共虚拟字符串姓
{
get;
set;


$ b public class EmployeeMap:ClassMap
{
public EmployeeMap()
{
Id(e => e.Id);
地图(e => e.Name);
地图(e => e.Surname);



public class DB
{
private static ISessionFactory mySessionFactory = null;

private static ISessionFactory SessionFactory
{
get
{
if(mySessionFactory == null)
{
mySessionFactory =流利.Configure()
.Database(MsSqlCeConfiguration.Standard
.ConnectionString(Data Source = MyDB.sdf))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
返回mySessionFactory;


$ b $ private static void BuildSchema(Configuration configuration)
{
SchemaExport schemaExport = new SchemaExport(configuration);
schemaExport.Execute(false,true,false);

$ b $ 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);




$ b $ div class =h2_lin>解决方案在使用NHibernate和SQL CE标识列时有一个bug。有两个解决方法,我知道:
$ b $ 1设置 connection.release_mode 属性为 on_close 在配置中:

  mySessionFactory =流利的.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 - 在事务中执行插入:

<使用(ISession session = DB.OpenSession())
using(ITransaction txn = session.BeginTransaction())
{
session.Save(pre $)雇员);
txn.Commit();
}

我意识到这个问题已经超过一个月了,但我希望这个答案是仍然对你有用。


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);
            }
        }
    }
}

解决方案

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.

这篇关于AssertionFailure:“空标识符” - FluentNH + SQLServerCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆