因此,我按照 ADO.NET 团队博客中的说明尝试制作一个小型测试项目。 我已经仔细检查了一切。它似乎不起作用,并一直说缺少连接字符串。
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx
步骤。 1个 构建此 UserModels.dll。在dll,App.Config文件中,edmx生成了这个连接字符串:(制作时点击'test'按钮,连接成功,并生成'UserDatabase'中所有表的edmx图)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="UserModelsContainer" connectionString="metadata=res://*/UserModels.csdl|res://*/UserModels.ssdl|res://*/UserModels.msl;provider=System.Data.SqlClient;provider connection string="data source=MyDesktop\SQL2008;initial catalog=UserDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
第 2 步。 然后我做了一个测试项目:
class UnitTetst1 ....
TestMethod1()....
using (var db = new UserModelsContainer()) {
int i = db.Users.Count(); // <---expecting '0' for a new db, but I get an exception
}
----------这里有问题 ------------------
第 3 步。运行测试。然后我收到这样的错误 InvalidOperationException:
"No connection string named 'UserModelsContainer' could be found in the application config file."
似乎 DbContext 不知道从哪里获取 App.Config 的 connectionStrings?
求助~~
最佳答案
运行程序时,读取的是正在运行的 .exe 文件的 app.config。从未使用过 .dll 的 app.config。由于 UserModel.dll 是一个 dll,因此您运行的某处必须有一个 .exe(或网站)。将连接字符串放在该 exe 的 app.config 中(或者如果它是 web.config 中的网站)。
关于entity-framework - 未看到 app.config/web.config 中的 Entity Framework DbContext 连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772108/