Mats
Mats

Reputation: 59

Fluent NHibernate from appSettings

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

Is there someone who can explain how should the file app.config look like?

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

And this is my connectionsString

Data Source=(local);Initial Catalog=ABC;Integrated Security=True

This doesn't work:

<appSettingKey>"Data Source=.;Initial Catalog=ABC;Integrated Security=True"</appSettingKey>

// Mats, Stockholm, Sweden

Upvotes: 3

Views: 4805

Answers (3)

James Gregory
James Gregory

Reputation: 14223

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

Upvotes: 1

JDPeckham
JDPeckham

Reputation: 2524

Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008.ConnectionString(
                                c => c.FromConnectionStringWithKey(connectStringKey)
                            )//End ConnectionString
                        )//End Database
                .Mappings(m =>m.FluentMappings.AddFromAssemblyOf<ADomainClassType>())
                .BuildSessionFactory();

This is how I build my session factory.

Upvotes: 0

Erik &#214;jebo
Erik &#214;jebo

Reputation: 10851

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")

Upvotes: 9

Related Questions