【问题标题】:Mixing DB configuration from file with Fluent NHibernate mapping将文件中的数据库配置与 Fluent NHibernate 映射混合
【发布时间】:2011-09-20 14:38:07
【问题描述】:

我偶然发现了以下问题:我想从配置文件配置数据库,但映射流畅(喜欢它!)配置代码如下所示:

var cfg = new Configuration();
cfg.Configure();
var fluentCfg = Fluently.Configure(cfg)
                        .Mappings(
                            m => m
                               .FluentMapping
                               .AddFromAssembly(Assembly.GetExecutingAssembly));

但是配置文件有一个属性:

<property name="proxyfactory.factory_class">
  NHibernate.ByteCode.LinFu.ProxyFactoryFactory, 
  NHibernate.ByteCode.LinFu
</property>

在 cfg.Configure(); 之后一切看起来都不错,配置指向 LinFu 字节码提供程序,但在第三行之后我看到配置更改为使用 Castle。我查看了 Fluent 的代码,我可能错了,但看起来他们在 PersistenceConfiguration 的构造函数中覆盖了 PersistenceConfiguration.cs(第 50 行)中的这个属性:

values[ProxyFactoryFactoryClassKey] =  DefaultProxyFactoryFactoryClassName;

Fluent 需要 Castle 吗?或者可能是我做错了什么,或者这只是一个错误?

谢谢。

【问题讨论】:

    标签: nhibernate fluent-nhibernate


    【解决方案1】:

    我不知道这是否是您正在寻找的,但它可能会帮助您。您可以在代码中公开配置并进行所需的任何更改。

    var cfg = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("ConnectionStringName")).ShowSql())
                    .Mappings(m =>
                    {
                        m.FluentMappings.AddFromAssemblyOf<MapMarker>();
                        m.FluentMappings.Conventions.AddFromAssemblyOf<ConventionMarker>();
                    })
                    .ExposeConfiguration(x => x.SetProperty("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu"));
    

    【讨论】:

    • 当然,但我的想法是从配置文件配置数据库(以便我可以在部署期间更改 conn.strings、db-s 等)并将映射保留为流利的(拥有一个很好的强类型,编译时可验证的映射)。
    【解决方案2】:

    有一个ProxyFactoryFactory 方法链接到Configure

    Fluently.Configure()
      .ProxyFactoryFactory(name);
    

    如果您不在 1.2 上,我相信它在 Database 电话下(请参阅 RexM 的回答)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      相关资源
      最近更新 更多