留给后人,这是我落得这样做:
1)抓起NHibernate的SchemaExporter源代码,并改名为类“OurSchemaExporter”。
2)增加了一个新的构造:
public OurSchemaExporter(Configuration cfg, Func<string, bool> shouldScriptBeIncluded)
: this(cfg, cfg.Properties)
{
this.shouldScriptBeIncluded = shouldScriptBeIncluded ?? (s => true);
}
3)改性initialize方法叫出以找出一个脚本是否应包含:
private void Initialize()
{
if (this.wasInitialized)
return;
if (PropertiesHelper.GetString("hbm2ddl.keywords", this.configProperties, "not-defined").ToLowerInvariant() == Hbm2DDLKeyWords.AutoQuote)
SchemaMetadataUpdater.QuoteTableAndColumns(this.cfg);
this.dialect = Dialect.GetDialect(this.configProperties);
this.dropSQL = this.cfg.GenerateDropSchemaScript(this.dialect);
this.createSQL = this.cfg.GenerateSchemaCreationScript(this.dialect);
// ** our modification to exclude certain scripts **
this.dropSQL = new string[0]; // we handle the drops ourselves, as NH doesn't know the names of all our FKs
this.createSQL = this.createSQL.Where(s => shouldScriptBeIncluded(s)).ToArray();
this.formatter = (PropertiesHelper.GetBoolean("format_sql", this.configProperties, true) ? FormatStyle.Ddl : FormatStyle.None).Formatter;
this.wasInitialized = true;
}
4)当使用OurSchemaExporter,通过查看每个SQL创建脚本的内容来检查是否要包含某些表:
var ourSchemaExport = new TpsSchemaExporter(configuration, ShouldScriptBeIncluded);
...
private bool ShouldScriptBeIncluded(string script)
{
return !tablesToSkip.Any(t => ShouldSkip(script, t));
}
private static bool ShouldSkip(string script, string tableName)
{
string s = script.ToLower();
string t = tableName.ToLower();
return
s.Contains(string.Format("create table dbo.{0} ", t))
|| s.Contains(string.Format("alter table dbo.{0} ", t))
|| s.EndsWith(string.Format("drop table dbo.{0}", t));
}
黑客,但它做的工作。
我不这么认为。 –