3

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.

CarlesD
  • 116
  • 6
  • your solution works for me, you probably have problem elsewhere, try checking inner exception. – mentat Nov 30 '11 at 16:36
  • Something like this should work:-.ConnectionString(ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString) – Nick Ryan Dec 20 '11 at 17:02

1 Answers1

1

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();