【问题标题】:Website is not taking connection string from web.config. it is searching for app.config网站未从 web.config 获取连接字符串。它正在搜索 app.config
【发布时间】:2013-08-31 07:20:44
【问题描述】:

我的项目具有 3 层结构 DataAccessLayer、BusinessLogicLayer 和网站。

在我的 DataAccessLayer 中,我使用了 F# 库项目,通过 app.config 文件访问连接字符串

F# 代码 -

type dbSchema = SqlDataConnection<"","MyConnection">

let connectionString = System.Configuration.ConfigurationManager.ConnectionStrings.               ["MyConnection"].ConnectionString 

App.config 代码 -

  <connectionStrings>
      <add name="MyConnection" connectionString="Data Source=MyServer;Initial   Catalog=MyDB;Persist Security Info=True;User ID=sa;Password=xyz;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

现在我已经给出了这个dll在网站BusinessLogic和网站项目中的引用。

我正在调用BusinessLogicLayer的一个函数来获取数据-

var MyDataList = BusinessLogic.GetAllData().ToList();

现在的问题是,网站正在 app.config 文件中搜索连接字符串,而不是 web.config。我希望它从 web.config 中获取连接字符串

【问题讨论】:

  • 您的 app.config 在您的“DataAccessLayer”项目中吗?
  • F# 项目是否在它自己的进程中运行?如果一切都从 IIS 运行,它应该从 web.config 读取。您确定您的 web.config 中有 &lt;connectionStrings&gt; &lt;add name="MyConnection" 部分吗?
  • 是的,该部分已添加到 web.config 中
  • 我怀疑这不起作用,因为 F# 编译器需要在编译时而不是运行时访问连接字符串。但我可能错了。
  • 它与在 web 应用程序目录中添加 app.config 一起工作。但不是 web.config。

标签: c# asp.net f#


【解决方案1】:

您可以为 Web 项目使用 web.config,也可以为应用程序使用 app.config。

您也可以加载自定义配置文件,另请参阅http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource.aspx

【讨论】:

  • 感谢您的快速回复。实际上它自动没有与 web.config 建立连接。我只想在 web.config 中定义连接字符串。
【解决方案2】:

您可以使用以下代码行获取连接字符串 -

using System;
using System.Configuration;

var connectionString=ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

这是一个参考: Connection String from App.config

【讨论】:

    【解决方案3】:

    这是一个老问题,但在搜索中它仍然出现在这个问题之前: F# Type Provider for SQL in a class

    在您的 F# 数据访问中,添加这样的函数

    let getDbContext connString = 
        match connString with
        | "" -> Sql.GetDataContext()
        | _ -> Sql.GetDataContext(connString)
    

    然后在 C# 代码中,您可以调用 getDbContext 并在运行时从 web.config 传入连接字符串。

    请注意,F# 数据访问层仍需要为编译器提供连接字符串以完成工作。

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      相关资源
      最近更新 更多