C# 在应用程序配置文件中找不到名为“MyEntities”的连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12622408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No connection string named 'MyEntities' could be found in the application config file
提问by jjc99
I am using entity framework and ASP.NET MVC 4 to build an application
我正在使用实体框架和 ASP.NET MVC 4 来构建应用程序
My solution is split into two projects;
我的解决方案分为两个项目;
- A class library that includes my data model (.edmx) file and a few custom interfaces
- The 'container' MVC project that references the class library above
- 包含我的数据模型 (.edmx) 文件和一些自定义接口的类库
- 引用上述类库的“容器”MVC 项目
My problem is that when I attempt to use the 'MyEntites' DbContextI get the the following error:
我的问题是,当我尝试使用“MyEntites” DbContext 时,出现以下错误:
No connection string named 'MyEntities' could be found in the application config file.
在应用程序配置文件中找不到名为“MyEntities”的连接字符串。
I guess the problem has something to do with the fact that connection string lies within the app.config of the class library rather than the MVC project.
我猜这个问题与连接字符串位于类库的 app.config 而不是 MVC 项目中的事实有关。
Does anyone have any suggestions?
有没有人有什么建议?
采纳答案by Jerry
Try copying the connections string to the .config file in the MVC project.
尝试将连接字符串复制到 MVC 项目中的 .config 文件。
回答by podiluska
As you surmise, it is to do with the connection string being in app.config of the class library.
正如您猜测的那样,这与类库的 app.config 中的连接字符串有关。
Copy the entry from the class app.config to the container's app.configor web.configfile
将 app.config 类中的条目复制到容器的app.config或web.config文件中
回答by Diego
are you using more than one project on your solution?
您是否在解决方案中使用了多个项目?
Because if you are, the web config you must check is the one on the same project as de .edmx file
因为如果是,则必须检查的 web 配置与 de .edmx 文件位于同一项目中
回答by Oren
You are right, this happens because the class library (where the .edmx file) is not your startup / main project.
您说得对,这是因为类库(.edmx 文件所在的位置)不是您的启动/主项目。
You'll need to copy the connection string to the main project config file.
您需要将连接字符串复制到主项目配置文件中。
Incase your startup / main project does not have a config file (like it was in my Console Application case) just add one (Startup project - Add New Item -> Application Configuration File).
如果您的启动/主项目没有配置文件(就像在我的控制台应用程序案例中一样),只需添加一个(启动项目 - 添加新项目 -> 应用程序配置文件)。
More relevant information can be found here: MetadataException: Unable to load the specified metadata resource
更多相关信息可以在这里找到: MetadataException: Unable to load the specified metadata resource
回答by Serj Sagan
You could just pass the connection string to EntityFrameworkand get on with your life:
您可以将连接字符串传递给EntityFramework并继续您的生活:
public partial class UtilityContext : DbContext
{
static UtilityContext()
{
Database.SetInitializer<UtilityContext>(null);
}
public UtilityContext()
: base("Data Source=SERVER;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD;MultipleActiveResultSets=True")
{
}
// DbSet, OnModelCreating, etc...
}
回答by Mehmedov
Add ConnectionString to MVC Project Web.config file
将 ConnectionString 添加到 MVC Project Web.config 文件
回答by Chalky
Yeah, it's silly. You can avoid copying the connection string by using a connection builder. VB.Net code (used in production, but slightly modified here, so treat as untested, happy to assist with any issues), where I have a serverName variable, a databaseName variable, I pass them into a method and have it generate the connection for me:
是的,这很愚蠢。您可以使用连接构建器来避免复制连接字符串。VB.Net 代码(在生产中使用,但在这里略有修改,因此视为未经测试,乐于帮助解决任何问题),其中我有一个 serverName 变量,一个 databaseName 变量,我将它们传递给一个方法并让它生成连接为了我:
Dim EfBuilder As New System.Data.EntityClient.EntityConnectionStringBuilder("metadata=res://*/VMware.VmEf.csdl|res://*/VMware.VmEf.ssdl|res://*/VMware.VmEf.msl;provider=System.Data.SqlClient;provider connection string=""data source=none;initial catalog=none;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""")
Dim SqlBuilder As New Data.SqlClient.SqlConnectionStringBuilder(EfBuilder.ProviderConnectionString)
SqlBuilder.DataSource = serverName
SqlBuilder.InitialCatalog = databaseName
EfBuilder.ProviderConnectionString = SqlBuilder.ConnectionString
Using vmCtx As New VmEfConn(EfBuilder.ConnectionString)
回答by saun4frsh
Add Connectoinstrnig in web.config file
在 web.config 文件中添加 Connectoinstrnig
<ConnectionStiring> <add name="dbName" Connectionstring=" include provider name too" ></ConnectionStiring>
回答by Mike Beeler
The connection string generated by the project containing the .edmx file generates the connection string, this would appear to be a holdover from the app.config sorts of files that were copied to the output directory and referenced by the executable to store runtime config information.
包含 .edmx 文件的项目生成的连接字符串生成连接字符串,这似乎是 app.config 类型文件的保留,这些文件已复制到输出目录并由可执行文件引用以存储运行时配置信息。
This breaks in the web project as there is no automatic process to add random .config information into the web.config file for the web project.
这在 web 项目中会中断,因为没有自动过程将随机 .config 信息添加到 web 项目的 web.config 文件中。
Easiest is to copy the connection string from the config file to the connections section of the web.config file and disregard the config file contents.
最简单的方法是将连接字符串从配置文件复制到 web.config 文件的连接部分并忽略配置文件内容。
回答by W92
If you have multiple projects in solution, then setUp project as started where you have your truth App.config.
如果您在解决方案中有多个项目,那么在您拥有真实 App.config 的地方开始设置项目。

