Skip to content

Instantly share code, notes, and snippets.

@brendanjerwin
Created March 19, 2009 17:06
Show Gist options
  • Save brendanjerwin/81939 to your computer and use it in GitHub Desktop.
Save brendanjerwin/81939 to your computer and use it in GitHub Desktop.
An example of how to get NHibernate to spit out database schema update scripts.
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using NHibernate.Dialect;
using NHibernate.Tool.hbm2ddl;
using StructureMap.Attributes;
namespace SchemaUpdateGenerator
{
class Program
{
static void Main(string[] args)
{
NHibernate.Cfg.Configuration cfg = null;
//Configure NHibernate here
var dialect = Dialect.GetDialect(cfg.Properties);
string[] schemaUpdateScript;
using (var conn = new SqlConnection(
cfg.GetProperty("connection.connection_string")))
{
conn.Open();
schemaUpdateScript = cfg.GenerateSchemaUpdateScript(dialect,
new DatabaseMetadata(conn, dialect));
}
var f = new FileInfo("schema_update.sql");
using (var stream = f.OpenWrite())
{
var writer = new StreamWriter(stream);
foreach (var s in schemaUpdateScript)
{
writer.WriteLine(s);
writer.WriteLine("GO;");
Console.WriteLine(s);
Console.WriteLine("GO;");
}
writer.Flush();
stream.Flush();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment