如何将连接字符串传递给类库?

时间:2014-01-27 06:45:51

标签: c# asp.net connection-string

我有一个像

这样的项目结构
MySolution
    -FrontEnd
         -webforms
         -console
         -Mvc
    -Repositories
         -Dapper   (Class library)
    -Tests

如何引用存储库类库中的连接字符串?我尝试在我的类库项目中添加App.Config,但它在Add New Items中不可用。我正在使用Visual Studio 2013 express for web。谢谢,Damien。

2 个答案:

答案 0 :(得分:5)

适用于库的.Config是使用这些库的执行程序集之一,因此您不必将App.Config添加到项目中,它将使用您的启动项目的App或Web.Config

要访问ConfigurationManager,您需要在库项目中添加对System.Configuration的引用。

答案 1 :(得分:2)

在FrontEnd项目中使用web.config。不要在存储库项目中使用app.config。

本文介绍如何从web.config中读取值。

http://www.connectionstrings.com/store-connection-string-in-webconfig/