Created
March 19, 2009 17:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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