NHibernate SchemaExport.Execute不创建表

时间:2015-06-16 04:18:01

标签: c# nhibernate nunit nhibernate-configuration

按照本教程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 个答案:

答案 0 :(得分:1)

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

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