NHibernate SchemaExport.Execute does not create table

1.3k views Asked by At

Learning NHibernate by following this tutorial Your first NHibernate based application and I got to the point where you call new SchemaExport(cfg).Execute(true, true, false); in a test method to export the schema (create the Product table) for verifying NHibernate was set up correctly

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

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

The mapping

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

The configuration

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

The test passed and I can even see the output sql that create table but the table is not being created in the database (sql server compact), I refreshed the db in server explorer and it is still empty.

I have checked these posts
NHibernate SchemaExport not creating table
NHibernate SchemaExport does not create tables when “script” is false
NHibernate does not create Tables
but none of them solve my problem. any ideas?

1

There are 1 answers

0
Radim Köhler On BEST ANSWER

I would suggest to use full path in the connection string:

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

Where the "C:\MyFirstNHibernateProject\" should be the full path to the "MyFirstNHibernateProject.sdf"

Also, in case you are suing CE 4.0 I would suggest to use this dialect:

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