Creating tables with NHibernate

2019-08-08 21:39发布

问题:

I am attempting to use NHibernate for a new application. The goal is to create tables with NH. I have two questions:

  1. How is this done, if it can be done.

  2. How does this change my configuration section in the app.config?

回答1:

Sounds like you are looking to do a SchemaExport

Example:

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

Where cfg is your NHibernate configuration object.

This is covered well about 1/3 of the way down on this NHibernate Forge article: http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx

The text their reads:

The fourth line of code uses the SchemaExport helper class of NHibernate to auto-"magically" generate the schema in the database for us. SchemaExport will create the product table in the database and each time you call it it will delete the table and the table data and recreate it.



回答2:

Look at NHibernate Fluent: http://wiki.fluentnhibernate.org/Getting_started