AssertionFailure:“null identifier” - FluentNH + SQLServerCE

时间:2010-03-02 08:31:37

标签: fluent-nhibernate sql-server-ce

代码在

session.Save(employee);
处失败,并带有AssertionFailure“null identifier”。我做错了什么?

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

2 个答案:

答案 0 :(得分:28)

将NHibernate与SQL CE标识列一起使用时存在“错误”。我知道有两种解决方法:

1 - 在配置中将connection.release_mode属性设置为on_close

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 - 在交易中执行插入:

    using (ISession session = DB.OpenSession())
    using (ITransaction txn = session.BeginTransaction())
    {
        session.Save(employee);
        txn.Commit();
    }

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

答案 1 :(得分:1)

仍然是真的。即使第一次调用SELECT @@ IDENTITY也会返回corect值,但秒返回null

我用

* connection.driver_class = NHibernate.Driver.SqlServerCeDriver 方言:NHibernate.Dialect.MsSqlCeDialect *

添加属性后 * connection.release_mode:on_close * 我的NHIbernate配置工作