FluentNHibernate won't merge config-properties with fluently mapped ones?

juggernaut78's Avatar

juggernaut78

12 Mar, 2010 11:47 AM via web

This is a 2-divided problem:

1) Connection string + fluently connection info.
Let's say I have a connection string I read from a global config such as:

<add key="DbConnectionString" value="Data Source=SomeServer;User ID=SomeUser;Password=SomePassword;Integrated Security=False;Pooling=True"/>

Then in every app I have defined a database to use:

I then would like to configure this in FNH by first setting connection string, and then setting the overriden properties like so:

var fnhCfg = FluentNHibernate.Cfg.Fluently.Configure();
fnhCfg.Database(MsSqlConfiguration.MsSql2005
   //Set default values            
  .ConnectionString(connectionString)
  //Set specific values            
  .ConnectionString(c => c
      .Database(DbNameReadFromConfig)

This won't work however since the "specific values" overrides the values in the connection string (thus making everything except Database to null.

2) NHibernate config properties + fluently properties

In config file I have:

<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="MyApp">
        <property name="current_session_context_class">managed_web</property>
    </session-factory>
</hibernate-configuration>

In code I call:

//After call below the properties from my config file exists in fnhCfg
var fnhCfg = FluentNHibernate.Cfg.Fluently.Configure();
//After call below the properties has been overwritten, and my property current_session_context_class is not to be found any longer.
fnhCfg.Database(<my db config>);

Is this "by design" or is it something that can be implemented further down the road?
Thanks,

  1. Support Staff 2 Posted by James Gregory on 12 Mar, 2010 12:02 PM

    James Gregory's Avatar

    What you can do is create an instance of the NHibernate Configuration class and pass that into Fluently.Configure. That will load the values from the config file, then merge it with what you specify in the fluently call.

    var cfg = new Configuration();
    
    Fluently.Configure(cfg)
      // other stuff
    
  2. 3 Posted by juggernaut78 on 12 Mar, 2010 12:23 PM

    juggernaut78's Avatar

    According to code base in 1.0.0.631 that is the same behavior when using default Configure().
    public class FluentConfiguration
    ...

        internal FluentConfiguration()
            : this(new Configuration())
    

    (And it did not solve the problem unfortunately). I.e. the .Database() call after I've run Configure([cfg]) seems to reset my nh-properties:

            var fileConfigSettings = new NHibernate.Cfg.Configuration();
            var fnhCfg = FluentNHibernate.Cfg.Fluently.Configure(fileConfigSettings);
            Debug.Assert(
                fileConfigSettings.Properties.ContainsKey("current_session_context_class"));
            fnhCfg.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.
                                ConnectionString(
                                "Data Source=MyServer;Initial Catalog=MyDb;User ID=MyUser;Password=MyPwd;Integrated Security=False;Pooling=True"));
            //Assert below fails
            Debug.Assert(
                fileConfigSettings.Properties.ContainsKey("current_session_context_class"));
    

    I guess that this (implementation wise) is completely unrelated to issue 1) though?

Reply to this discussion

Preview Comments are parsed with Markdown. Help with syntax

Attached Files

    You can attach files up to 10MB

    How many minutes are in an hour?