Skip to main content

Posts

Showing posts from July, 2010

NHibernate: Using multiple database schemas with SchemaExport

Let's say you want to use NHibernate, and have tables on multiple schemas on your database. I'm using SQL Server, and my quick solution is not supported by all the databases NH supports, but it shouldn't be hard to change. If you run the SchemaExport, you will receive an error telling you that the DB schema does not exist or you have no access to it. It makes sense since the utility does not handle multiple schemas. Well, here's a simple code snipped I've written after debugging and digging into the NH source code: private void CreateSchema() { var provider = ConnectionProviderFactory.NewConnectionProvider(NHConfigs.Properties); using (var connection = provider.GetConnection()) { using (var cmd = connection.CreateCommand()) { cmd.CommandText = "IF (SCHEMA_ID('your_schema_name') IS NULL) EXEC('CREATE SCHEMA your_schema_name')" ; cmd.ExecuteNonQuery(); } provider.CloseConnect