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.

2
  • your solution works for me, you probably have problem elsewhere, try checking inner exception. Commented Nov 30, 2011 at 16:36
  • Something like this should work:-.ConnectionString(ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString) Commented Dec 20, 2011 at 17:02

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.