为了与recommendation here保持一致,我们将连接字符串分隔为xml文件。我们的web.config:
//old - works
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;"
providerName="System.Data.SqlClient" />
<connectionStrings>
//new - fails
<connectionStrings configSource="connections.config" />
connections.config的内容:
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;Initial Catalog=xxx;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
这两个文件都位于项目根文件夹中。 EntityFramework适用于这两种解决方案。但是当我尝试获取某些测试的连接字符串时,它无法检索。这是我们用来提取连接字符串的代码:
string ConnectionString =
ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
直接从web.config读取连接字符串,当我们引入ConfigSource时它失败了。我们需要做些什么呢?