首页 文章

NHibernate SchemaExport.Execute不创建表

提问于
浏览
2

按照本教程Your first NHibernate based application学习NHibernate,我在测试方法中调用 new SchemaExport(cfg).Execute(true, true, false); 以导出模式(创建Product表)以验证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
但他们都没有解决我的问题 . 有任何想法吗?

1 回答

  • 1

    我建议在连接字符串中使用 full path

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

相关问题