gpt4 book ai didi

c# - DLL 的 app.config 中的 ConnectionString 为空

转载 作者:行者123 更新时间:2023-11-30 13:20:58 25 4
gpt4 key购买 nike

我有一个类库,它在 app.config 中包含一个有效的 connectionString。在那个类库中,我想将它与

一起使用
ConfigurationManager.ConnectionStrings["NAME"].ConnectionString

我的 ASP.net 4.0 框架应用程序引用该 DDL 并从中检索数据。我想使用 App.config 中的 ConnectionString 在我的 DDL 中创建一个 Entity Framework 4 DataContext。 (我不想在每个方法中都从我的 ASP.net 应用程序传递 connectionString。(我正在使用 ObjectDataSources))

但是,我的 DLL 中的这一行抛出 NullReferenceException。

ConfigurationManager.ConnectionStrings["NAME"].ConnectionString

我该如何解决这个问题?

最佳答案

I have a class library that contains a valid connectionString inside the app.config

类库没有关联的 app.config 文件。它是使用此程序集的应用程序。因此,您需要将连接字符串放入此配置文件中(如果这是一个 ASP.NET 应用程序,则为 web.config)。因此,在 Visual Studio 中的类型类库项目中添加 App.config 文件是没有意义的。

关于c# - DLL 的 app.config 中的 ConnectionString 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3641896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com