Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate SchemaExport does not create tables when "script" is false

Making my first steps with NHibernate, I'm trying to have it creating my Tables automatically from the hbm files. The database backend is SQL Server 2008 Developer Edition.

This is the common sample code I see in NHibernate Tutorials:

var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Posting).Assembly);
new SchemaExport(cfg).Execute(false,true,false,false);

Sadly, this does not work. I have set show_sql to true, and it does not print out any statement. Looking at SQL profiler I see my Application connecting to the DB, but then doing nothing.

I can fix that by changing the first parameter ("script") to true:

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

I don't understand why. The parameters of SchemaExport are sadly not really explained (also not the difference between .Create and .Execute), and I would like to find out what this parameter does, and why it's not needed i.e. when using SQL Compact Edition (that works also when script is false)

like image 906
Michael Stum Avatar asked May 03 '09 15:05

Michael Stum


1 Answers

The SchemaExport is part of the Hbm2Ddl utility which is really separate from NHibernate functionality. It does not use "show_sql" which is used while NHibernate is running only.

To get a copy of the schema you create you use .SetOutputFile(filename)

This is the method I use when I wish to create a new database. I get a formatted schema in MyDDL.sql file and the database is built from the schema:

 private void BuildSchema(Configuration config)
 {

        new SchemaExport(config)
            .SetOutputFile(_fileName + "MyDDL.sql")
            .Execute(true /*script*/, true /*export to db*/,
                     false /*just drop*/, true /*format schema*/);
 }

SchemaExport.Create is just a shortcut to Schema.Execute with the just drop false and format true.

public void Create(bool script, bool export)
    {
        Execute(script, export, false, true);
    }
like image 123
Maggie Avatar answered Oct 21 '22 05:10

Maggie