实体框架DbContext app.config / web.config中的连接字符串未被看到 [英] Entity Framework DbContext Connection string in app.config/web.config not being seen
问题描述
我已经仔细检查了一切。它似乎不起作用,不断说连接字符串丢失。
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.0encoding =utf-8?>
< configuration>
< connectionStrings>
< add name =UserModelsContainerconnectionString =metadata = res://*/UserModels.csdl | res://*/UserModels.ssdl | res://*/UserModels.msl; provider = System .Data.SqlClient; provider connection string =& quot; data source = MyDesktop\SQL2008; initial catalog = UserDatabase; integrated security = True; multipleactiveresultsets = True; App = EntityFramework& quot; 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 a get a exception
}
---------这里有问题-----------------
步骤3运行测试。然后我收到一个错误InvalidOperationException,如下所示:
应用程序配置文件中没有找到名为UserModelsContainer的连接字符串。
看起来像DbContext不知道从App.Config ??中获取connectionStrings? p>
请帮助~~
运行程序时,这是应用程序正在运行的.exe文件的.config文件被读取。 .dll的app.config从不使用。由于UserModel.dll是一个DLL,所以运行的地方必须有一个.exe(或网站)。将连接字符串放在该exe的app.config中(或者它是web.config中的一个网站)。
So, I have followed this instruction from ADO.NET team blog to try to make a small test project. I have double-checked everything. It doesn't seem to work and keeps saying connection string is missing.
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx
Step. 1 Build this UserModels.dll. In the dll, App.Config file, edmx generated this connection string: (hit the 'test' button when making it, and it connects successfully, and generated the edmx diagram of all the tables from 'UserDatabase')
<?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>
Step 2. Then I made a test project:
class UnitTetst1 ....
TestMethod1()....
using (var db = new UserModelsContainer()) {
int i = db.Users.Count(); // <---expecting '0' for a new db, but I get an exception
}
---------PROBLEM HERE -----------------
Step 3. Run the test. Then I get an error InvalidOperationException like this:
"No connection string named 'UserModelsContainer' could be found in the application config file."
Seems like DbContext doesn't know where to pick up the connectionStrings from App.Config??
Please help~~
When running a program, it's the app.config of the .exe file being run that is read. The app.config of the .dll is never used. Since UserModel.dll is a dll, there must be an .exe (or web site) somewhere that you run. Place the connection string in that exe's app.config (or if it is a web site in the web.config).
这篇关于实体框架DbContext app.config / web.config中的连接字符串未被看到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!