【问题标题】:Fluent nHibernate: Use the same mapping files for tables with the same structure in different schemasFluent nHibernate:对不同架构中结构相同的表使用相同的映射文件
【发布时间】:2019-07-05 00:45:21
【问题描述】:

这是我的映射类:

class MyTableMap : ClassMap<MyTable>
{
    public MyTableMap()
    {
        Schema("mySchema");
        Id(x => x.id);
        Map(x => x.SomeString);
    }
}           

这适用于我的第一个数据库中的表 ([mySchema].[MyTable])。

但是这个表(“MyTable”)存在于(实际上很多)不同的数据库中,但是出于任何原因,架构总是被命名为不同的(这我没有任何控制权):

所以在数据库“OtherDB”中有一个表 [SomeOtherSchema].[MyTable],其结构与第一个数据库中的 [mySchema].[MyTable] 相同。

出于显而易见的原因,我不想为每个数据库创建不同的映射类。

那么:有没有办法改变映射类的架构,所以我只需要创建一个映射类(不使用单例!)?

【问题讨论】:

    标签: c# fluent-nhibernate schema


    【解决方案1】:

    看来我必须使用“DefaultSchema”。所以我使用了这个映射代码:

    class MyTableMap : ClassMap<MyTable>
    {
        public MyTableMap()
        {
            Id(x => x.id);
            Map(x => x.SomeString);
        }
    }           
    

    当我构建 sessionFactory 时,我必须设置 DefaultSchema:

    var configure = Fluently.Configure();
    var dbConfig = MsSqlConfiguration.MsSql2012.ConnectionString("Data Source=" + dataSource +
                                                                ";Initial Catalog=" + database +
                                                                ";Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
    
    //Here I can set the default schema used by my mappings
    var dbConfigWithSchema = dbConfig.DefaultSchema(database);  
    var fluentDb = configure.Database(dbConfigWithSchema);
    
    var fluentMap = fluentDb.Mappings(mappings);
    return fluentMap.BuildSessionFactory();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      相关资源
      最近更新 更多