我正在使用NHibernate作为ORM在VS中设置一个新项目。 我只是尝试通过在表中插入新对象来验证我的配置代码。
我的NH mapper xml模型:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHTest"
namespace="NHTest">
<class name="Product">
<id name="Id">
<generator class="identity" />
</id>
<property name="Name" />
<property name="Category" />
<property name="Discontinued" />
</class>
</hibernate-mapping>
型号.cs:
namespace NHTest
{
public class Product
{
virtual public Guid Id { get; set; }
virtual public string Name { get; set; }
virtual public string Category { get; set; }
virtual public bool Discontinued { get; set; }
}
}
示例代码:
var configuartion = new Configuration();
configuartion.DataBaseIntegration(x =>
{
x.ConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
x.Driver<SqlClientDriver>();
x.Dialect<MsSqlCeDialect>();
});
configuartion.AddAssembly("NHTest");
var sessionFactory = configuartion.BuildSessionFactory();
var os = sessionFactory.OpenSession();
os.Save(new Product()
{
Category = "X",
Discontinued = true,
Name = "Radek"
});
os.Flush();
问题:
1)为什么flush方法返回异常&#34;可以插入db ...&#34;?
2)是否与db?
的配置有关答案 0 :(得分:0)
更改
generator class =&#34; identity&#34;
到
generator class =&#34; guid.comb&#34;
你不能用Guids进行身份插入,需要生成客户端,guid.comb会为你生成一个guid。