在VS中使用本地Db设置NHibernate

本文关键字:Db 设置 NHibernate VS | 更新日期: 2023-09-27 18:07:05

我正在使用NHibernate作为ORM在VS中设置一个新项目。我只是试着通过在表中插入新的对象来验证我的配置代码。

My model xml for NH mapper:

<?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>

Model .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方法返回异常"could insert into db…"?

在VS中使用本地Db设置NHibernate

是否与db的配置有关?

变化

发电机类= "身份"

发电机类= " guid.comb "

你不能用guid来做标识插入,它们需要在客户端生成,guide .comb会为你生成一个guid