views:

86

answers:

3
+1  A: 

I may be interpreting the question wrong, it's not really clear what you are asking for.

Assuming you have created classes and configured NHibernate correctly and you want to create tables in the database for those classes, you have at least two potential ways to try to generate a database without creating NHibernate mappings, both of which will likely work much better with at least some hints about how to do the mappings:

Fluent NHibernate Automapper

ConfORM

There is a decent learning curve for both options.

Another option is to try one of the commercial visual designers for NHibernate, although those tools aren't quite mature enough to do this really well in my experience.

Core NHibernate is not designed or intended to create tables without mappings files.

Michael Maddox
This is close to what I was talking about. The database is already populated with tables and columns, I want to export the schema...In otherwords I want to do the reverse of this.
Damien
What format do you want the meta data / schema in? Do you want POCO classes plus NHibernate hbm.xml mapping files generated from the database? Or do you want the metadata in a different format like a SQL Script? Are you looking for a .NET API into database metadata like what SQL Server provides through SMO?
Michael Maddox
The .Net API would be excellent..although otherwise POCO plus nHibernate mappings would be handy. This is mostly experimental. I need to decide the best format. I thought that Database Metadata would work (ships with nHibernate)
Damien
+2  A: 

There are several tools to help you out but the two I use the most are the following two.

  1. NHibernate Schema Tool
  2. NHibernate Mapping Generator

If you already have a schema you can use the NHibernate Mapping Generator to create your mappings. You can then use the mappings for whatever you want. Modify them and use NHibernate Schema Tool to manage the actual schema.

If you don't have any schema and that is what you are trying to create you are on the right track. First you need to "map" your classes. Preferably using Fluent NHibernate or ConfORM like Michael Maddox suggested.

I don't know the purpose of this. If it is database schema management I would recommend against using NHibernate. NHibernate was never developed as a schema manager tool so it probably should not be used this way. Admittedly I might have misunderstood you somehow and this answer could be completely wrong.

mhenrixon
Just a heads up if you do end up using NHibernate Mapping Generator that you might have issues fetching all tables from your Oracle database. I used it yesterday and had to change what table was getting used to fetch the table names and constraints.
Mike
+2  A: 

NHibernate is actually based on the mapping files. You could generate classes or tables from them. There are tools to generate the mapping files, but they are based on the classes, not the tables.

Answers to your specific questions:

Approach one: NHibernate does not read table definitions from the database. All the table definitions need to be specified in the mapping files.

Approach two: SchemaExport creates an SQL file (Create tables, indexes etc) from the mapping definitions. It is actually recommended to use it, unless you need to cope with legacy databases. The output file should be called *.sql, not *.dll.

The error you get is most probably because you try to create an identity id on an oracle database (or another which does not support identity columns). Use hilo instead (or, if you don't like it, guid.comb or native). I just wonder why you get this error, I thought that you didn't write any mapping files?

Conclusion:

I don't know of any tool which create NHibernate mapping files from database tables. There may be one, most probably it is not free or not mature (because otherwise it would be well known). So I suggest to think about generating the table definitions instead, or, in case you have a legacy database, you need to go through writing the mapping files manually.

Stefan Steinegger
If his database only has a handful of fields I'd agree mapping everything out manually. Otherwise, grab a tool to at least generate the proper member names and mapping. Since most tools seem to have issues generating FK's (or his database might not use them if it's legacy) and relationship types, he might just have to set those up by hand.
Mike
Cheers. This helped get down to the bottom of the issue I was having.
Damien