c# - NHibernate SchemaExport.Execute 不创建表

标签 c# nhibernate nunit nhibernate-configuration

按照本教程学习 NHibernate Your first NHibernate based application我到了你打电话的地步 new SchemaExport(cfg).Execute(true, true, false); 在导出模式(创建产品表)以验证 NHibernate 是否正确设置的测试方法中

[Test]
public void Can_generate_schema()
{
    var cfg = new Configuration();
    cfg.Configure();
    cfg.AddAssembly(typeof(Product).Assembly);

    new SchemaExport(cfg).Execute(true, true, false);
}

映射

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyFirstNHibernateProject" namespace="MyFirstNHibernateProject.Domain">
  <class name="Product">
    <id name="Id">
      <generator class="guid"></generator>
    </id>
    <property name="Name"></property>
    <property name="Category"></property>
    <property name="Discontinued"></property>
  </class>
</hibernate-mapping>

配置

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="connection.connection_string">Data Source=MyFirstNHibernateProject.sdf</property>

    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

测试通过了,我什至可以看到创建表的输出 sql,但表没有在数据库中创建(sql server compact),我在服务器资源管理器中刷新了数据库,它仍然是空的。

我已经检查了这些帖子
NHibernate SchemaExport not creating table
NHibernate SchemaExport does not create tables when “script” is false
NHibernate does not create Tables
但他们都没有解决我的问题。 有什么想法吗?

最佳答案

我建议在连接字符串中使用完整路径:

// instead of this
<property name="connection.connection_string"
    >Data Source=MyFirstNHibernateProject.sdf</property>

// we should use 
<property name="connection.connection_string"
    >Data Source=C:\MyFirstNHibernateProject\MyFirstNHibernateProject.sdf</property>

"C:\MyFirstNHibernateProject\" 应该是 "MyFirstNHibernateProject.sdf" 的完整路径

此外,如果您要起诉 CE 4.0,我建议您使用这种方言:

<property name="dialect">NHibernate.Dialect.MsSqlCe40Dialect</property>

关于c# - NHibernate SchemaExport.Execute 不创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30858699/

相关文章:

c# - 向 WPF 窗口添加控件而不阻塞 GUI 线程

c# - 在哪里可以找到_简单_、易于理解的 LR(1) 解析器生成器的实现?

c# - Oracle ManagedDataAccess.EntityFramework Database.SqlQuery 按位置绑定(bind)参数?

c# - 如何编写处理单个对象和集合的通用 Save() 方法?

c# - 使用 Devart 或 NHibernate

c# - 如何以同步方式合并两个 TPL DataFlow 管道?

c# - NHibernate DateTime 毫秒精度

c# - 如何在 C# 中模拟基类属性或方法

c# - 如何使用多个数据源进行单元测试?

c# - 模拟C#中同一类中的方法