AssertionFailure:“空标识符” - FluentNH + SQL服务器CE

发布于 2024-08-24 00:45:33 字数 2278 浏览 5 评论 0原文

代码失败于

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-08-31 00:45:33

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

我意识到问题更多不到一个月了,但我希望这个答案对您仍然有用。

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.

菊凝晚露 2024-08-31 00:45:33

仍然是真实的。即使第一次调用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文