DLL 的 app.config 中的 ConnectionString 为空 [英] ConnectionString from app.config of a DLL is null

查看:32
本文介绍了DLL 的 app.config 中的 ConnectionString 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class library that contains a valid connectionString inside the app.config. Inside that class library I want to use it with

ConfigurationManager.ConnectionStrings["NAME"].ConnectionString

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

My ASP.net 4.0 framework application references that DDL and retrieves data from it. I want create a Entity Framework 4 DataContext within my DDL with the ConnectionString from the App.config. (I do not want to pass the connectionString from my ASP.net application in every single method. (I'm using ObjectDataSources))

但是,我的 DLL 中的这一行会引发 NullReferenceException.

However, this line inside my DLL throws a NullReferenceException.

ConfigurationManager.ConnectionStrings["NAME"].ConnectionString

我该如何解决这个问题?

How can I fix this issue?

推荐答案

我有一个类库,其中包含一个内的有效连接字符串应用程序配置

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

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

A class library doesn't have an app.config file associated. It's the application consuming this assembly that does. So you need to put the connection string inside this config file (if this is an ASP.NET application this would be web.config). Thus adding an App.config file in a project of type class library in Visual Studio makes no sense.

这篇关于DLL 的 app.config 中的 ConnectionString 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆