gpt4 book ai didi

fluent-nhibernate - 断言失败 : "null identifier" - FluentNH + SQLServerCE

转载 作者:行者123 更新时间:2023-12-03 21:18:35 24 4
gpt4 key购买 nike

代码在

session.Save(employee);
处失败带有 AssertionFailure “空标识符”。我究竟做错了什么?
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);
}
}
}
}

最佳答案

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

1 - 设置 connection.release_mode属性(property)到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();
}

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

关于fluent-nhibernate - 断言失败 : "null identifier" - FluentNH + SQLServerCE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com