views:

772

answers:

2

I want to configure my NHibernate Fluent from a app.config and a appSettingKey.

Is there someone who can explain how the file: app.config, will look like

MsSqlConfiguration.MsSql2005
.ConnectionString(c => c
.FromAppSetting("appSettingKey"));

And this is my connectionsString: Data Source=(local);Initial Catalog=ABC;Integrated Security=True

"Data Source=.;Initial Catalog=ABC;Integrated Security=True" <--- This doesn't work.

// Mats, Stockholm, Sweden

+3  A: 

If I understand you correctly, you wish to configure Fluent NHibernate as in your example and use a connection string from App.config. Below is an example of how I would accomplish that.

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="FluentNHibernateConnection"
      value="server=.;Initial Catalog=YourDB;Integrated Security=True" />
  </appSettings>
</configuration>

Code to create a session factory:

private static ISessionFactory CreateSessionFactory()
{
    var fluentConfig = MsSqlConfiguration.MsSql2005
        .ConnectionString.FromAppSetting("FluentNHibernateConnection");

    PersistenceModel persistenceModel = new PersistenceModel();
    persistenceModel.addMappingsFromAssembly(typeof(User).Assembly);

    Configuration nhConfig = new Configuration()
        .AddProperties(fluentConfig.ToProperties());

    persistenceModel.Configure(nhConfig);

    return nhConfig.BuildSessionFactory();
}

Hope it helps.

/Erik (a fellow "Stockholmare")

Erik Öjebo
+1  A: 

Have a read of Database Configuration in the Fluent NHibernate wiki.

James Gregory

related questions