0

我们使用Castle ActiveRecord作为NHibernate顶层的帮助器层。要生成我们的数据库中,我们使用:我可以使用Castle ActiveRecord构建我的数据库模式时排除某些表格

ActiveRecordStarter.CreateSchema(); 

来产生我们用于构建我们的数据库的SQL:

ActiveRecordStarter.GenerateCreationScripts(filename); 

他们的工作就像一个魅力。但是我们有时不想为每个实体生成一个表格。我相信nhibernate有一个从模式构建中排除某些表的机制(请参阅here) - 有谁知道ActiveRecord是否可以做同样的事情?

+0

我不这么认为。 –

回答

0

留给后人,这是我落得这样做:

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)); 
} 

黑客,但它做的工作。

+0

你应该接受你自己的答案。 – rbellamy

相关问题