Eren
Eren

Reputation: 117

Fluent NHibernate SQL Server 2012

I'm developing a .net project and I use the first time SQL Server 2012 with fluent nhibernate when I running my project I get the following error =>

Value cannot be null.

Parameter name: Data Source

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: Data Source

My configuration syntax

Fluently.Configure()
   .Database(
      MsSqlConfiguration.MsSql2012.ConnectionString(c => c.FromAppSetting("ConnectionString")))
      .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
      .CurrentSessionContext<T>()
      .BuildSessionFactory();

and my connection string in web.config:

<connectionStrings>
    <add name="ConnectionString" 
         connectionString="Data Source=localhost; Initial Catalog=ExcelReport; Integrated Security=true;" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

I've tried various ways but I couldn't fix the problem. Do you have any suggestion?

Upvotes: 3

Views: 6799

Answers (1)

Donal
Donal

Reputation: 32803

<connectionStrings> are not the same as <appSettings>.

You connection string is not in an app setting.

You connection string is in <connectionStrings>

You therefore need to use FromConnectionStringWithKey instead of FromAppSetting.

For example:

c => c.FromConnectionStringWithKey("ConnectionString")

Your configuration syntax should be:

Fluently.Configure()
   .Database(
      MsSqlConfiguration.MsSql2012.ConnectionString(c => c.FromConnectionStringWithKey("ConnectionString")))
      .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
      .CurrentSessionContext<T>()
      .BuildSessionFactory();

Upvotes: 2

Related Questions