Fluent NHibernate Table Dump Utility

Summary

So I’ve been posting a lot about NHibernate over the past few months (Fluent NHibernate to be exact). Mostly motivated by frustrations with Entity Framework. I have spent the past week working with NHibernate in a production environment as a proof of concept example. The first problem with NHibernate is the fact that you have to manually generate the classes and mappings. So I came up with a simple and dirty solution to this problem…

The Table Dump Utility

I threw together a console application that can be used to dump all the tables of designated databases into directories. Each table generates a “.cs” file with the class and mapping code in it. You can download the console application here:

GenerateNHibernateTables.zip

To use the utility, change the “ConnectionString” at the top to point to your database. Then you can change the parameter of the “DumpDatabase()” method to contain the name of the database you wish to generate tables for. You can add additional “DumpDatabase()” methods if you want to dump a bunch of databases. Each database will be contained in a subdirectory named the same as the database name. All the subdirectories will be contained inside a directory on your C: drive named “NHDatabase”. Obviously, you can hack up this program to suite your needs.

What this program will generate

This program will generate a “.cs” file for each table that will contain the class and mapping for the table. I have quickly thrown in some data types and what the matching class virtual getter will be (i.e. “int”, “string”, “DateTime”, etc).

Composite keys are recognized and the “Equals” and “GetHashCode” methods are automatically inserted into the class with the proper field names.

The “Table” mapping is included for convenience and can be altered. This is setup for MS SQL server, containing the database name as well as the table name.

Nullable and not Nullable fields are accounted for.

String lengths are accounted for.

What this program will not generate

No foreign key constraints are generated.

No one-to-many or many-to-many mappings are setup.

Only the most generic field types are recognized.

No context code is generated.

What can you use this program for?

I wrote this program to reduce some of my tedious data-entry work. You can get about 90% of the code you need for your Fluent mappings and table classes with this utility, then spend a few minutes cleaning up anything that doesn’t quite fit. If a field type is not recognized, then the program will spit out an “unknown(sql field name)” field name. You can decide what to name this variable and type over the unknown name.

Also, don’t forget to correct the namespace. I threw a generic “databasename.NameSpace” text for the name space. You should change this to match your project namespace. You can do it in the program before you generate tables to save yourself some typing.

Also, you can rerun this program to write over the tables that exist. Make sure you don’t have a directory open before you do it (otherwise you might lock the program out from making changes).

To use the files, just drag the “.cs” files into your project, tweak the namespace, correct any syntax errors and add your tables to your own context class.

That’s it!

Leave a Reply