Random Musings

June 13, 2009

nHibernate SchemaExport

Filed under: nhibernate — haditeo @ 3:02 pm

Today i spent some time to learn nHibernate for the first time. This article is really fabolous. Credit to Gabriel Schenker as the author.

When i followed the article, somehow i noticed that the ‘Category’ records that have been added to the database was wiped out. Very very strange indeed, i cannot find any exception araised, the nUnit tests are working properly.

My first investigation is to turn on the profiler and these results come up :

table is dropped

Please be aware that i have created the Category table in the database, so why this nHibernate drop my ‘Category’ table? That means somewhere inside the code, it re-creates the necessary table for me automatically.

Finally i found the culprit, after careful debugging and googling, this line is the culprit

public void SetupContext()
    new SchemaExport(_configuration).Execute(false, true, false, false);

The SchemaExport method automatically create the necessary tables for me based on the Domain that i have been created. This is cool ! I noticed that in the article, i didn’t even have to create a single table in the database. Fabolous

I have also found this article NHibernate:How to Build Great Mappings and use SchemaExport while googling for the SchemaExport method


Blog at WordPress.com.