CarlesD
CarlesD

Reputation: 116

Fluent nhibernate configuration error using web.config

Hi im new in fluent nhibernate: I get an error when configure db conection using web.config The error is "An invalid or incomplete configuration was used.."

Web.config:

    <connectionStrings>
       <add name="Connection1" providerName="System.Data.SqlClient"
        connectionString="Server=local;Database=aDataBase;User ID=aUser;Password=***;Trusted_Connection=False;"/>

My fluent configuration:

_sessionFactory = Fluently.Configure()
  .Database(MsSqlConfiguration.MsSql2008
        .ConnectionString(c => c.FromConnectionStringWithKey("Connection1")).ShowSql() )
        .Mappings(m =>m.FluentMappings.AddFromAssemblyOf<Car>())
            .BuildSessionFactory(); 

It works if I use

   .ConnectionString(@"Server=local;Database=aDataBase;User ID=aUser;Password=***;Trusted_Connection=False;"

but I want to get the connection string from Web.config (not Hard-coded).

thanks.

Upvotes: 3

Views: 1079

Answers (1)

Rafael Leites Luchese
Rafael Leites Luchese

Reputation: 89

Try using the ConfigurationManager object to acces your connection string. Something like this should work:

_sessionFactory = Fluently.Configure()
    .Database(MsSqlConfiguration.MsSql2008
    .ConnectionString(ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString).ShowSql())
    .Mappings(m =>m.FluentMappings.AddFromAssemblyOf<Car>())
    .BuildSessionFactory();

Upvotes: 2

Related Questions