Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 420
CarlesD Avatar asked Apr 08 '26 09:04

CarlesD


1 Answers

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();
like image 85
Rafael Leites Luchese Avatar answered Apr 23 '26 13:04

Rafael Leites Luchese



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!