c# - 流利的 NHibernate SQL Server 2012

标签 c# asp.net sql-server sql-server-2012 fluent-nhibernate

我正在开发一个 .net 项目,我第一次使用 SQL Server 2012 和流畅的 nhibernate,当我运行我的项目时,我收到以下错误 =>

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

我的配置语法

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

和我在 web.config 中的连接字符串:

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

我尝试了各种方法,但无法解决问题。你有什么建议吗?

最佳答案

<connectionStrings><appSettings> 不同.

您的连接字符串不在应用程序设置中。

您的连接字符串在 <connectionStrings>

因此您需要使用 FromConnectionStringWithKey而不是 FromAppSetting .

例如:

c => c.FromConnectionStringWithKey("ConnectionString")

你的配置语法应该是:

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

关于c# - 流利的 NHibernate SQL Server 2012,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26318769/

相关文章:

asp.net - 从中继器控件更新面板和触发器

c# - 解释 C# 堆栈跟踪

c# - 我可以更改内插字符串表达式的颜色吗?

c# - 如何从 byte[] 中的音频文件创建 AudioClip

javascript - JavaScriptSerializer 期间 ASP.NET MVC AngularJS 中的 MaxJsonLength 异常

asp.net - 禁止在字符串中间出现两个连续空格的正则表达式

c# - 将 SQL Server 数据从 c# 显示到 html 页面

sql - 如何创建 SELECT 查询 FROM "TABLE1 AND TABLE2"

sql-server - UNION 和字面值,奇怪的结果

c# - MvvmCross - 在 View 模型中单击 handle 按钮