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

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

问题描述

我有一个包含在app.config内有效的connectionString一个类库。里面的那个类库我想与使用

  ConfigurationManager.ConnectionStrings [NAME]。的ConnectionString

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

不过,此行我的DLL中抛出一个NullReferenceException。

  ConfigurationManager.ConnectionStrings [NAME]。的ConnectionString

我怎样才能解决这个问题?


解决方案

  

我有一个包含一个类库
  内部有效的connectionString
  的app.config


一个类库没有相关的app.config文件。这是耗时大会,不应用程序。因此,你需要把这个配置文件中的连接字符串(如果这是一个ASP.NET应用程序,这将是的web.config )。因此,在Visual Studio类型类库的项目添加的App.config 文件是没有意义的。

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

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))

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

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中的ConnectionString的app.config为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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