views:

2134

answers:

1

I want to use DDD in a new project and model my classes first, then generate the database schema based on the class library. My plan is to do this with the NHibernate hbm2ddl tool SchemaExport.

The problem is that I can't get the SchemaExport to work because of a weird catch-22 problem it puts me in. The SchemaExport requires a Configuration object that itself requires a valid NHibernate configuration file as well as a set of database mappings.

The catch-22 here is that when I do the Configure(), it complains "Could not determine the name of the table for entity 'MyEntity'; remove the 'table' attribute or assign a value to it." So the Configure() method requires the table to exist, while the SchemaExport is supposed to create it based on the Configuration that I can't create because the table isn't doesn't exist.

So, how on earth am I supposed to create a valid NHibernate Configuration containing the mappings required for SchemaExport to actually do something useful without having Configure() throw and complain that it can't find the tables that are to be created with SchemaExport? Is there a "mode" I can set the Configuration object in so it won't check the database for the existence of the given tables, or is there something else I need to do?

+4  A: 

Can you post your configuration file?

I use this method all the time with no tables present, and am able to generate the schema on the fly. My guess is that you may have something off in one of your .hbm files. Try cutting your schema down to 1 table, getting it to work, then building it up from there. As a reference, here is the code I use to generate the db schema:

    var cfg = new Configuration();
    cfg.Configure();
    var schema = new SchemaExport(cfg);
    schema.Create(true, true);

This will also push the script to the console for you, so you can see what SQL is generated against the db.

Mark Struzinski
Thanks for the tip. Since I don't write the HBM myself, but get it generated dynamically by Fluent NHibernate, I didn't notice that I had an error in the "GetTableName" convention that caused the 'table' attribute in the resulting HBM to be set to an empty string.Getting my hands on the generated HBM files made the problem very clear and I was able to fix it within a number of seconds. Yay! :)
asbjornu