我使用实体框架和ASP。NET MVC 4构建应用程序

我的解决方案分为两个项目;

一个类库,其中包括我的数据模型(.edmx)文件和一些自定义接口 引用上面类库的“容器”MVC项目

我的问题是,当我试图使用'MyEntites' DbContext时,我得到以下错误:

中找不到名为“MyEntities”的连接字符串 应用程序配置文件。

我猜这个问题与连接字符串位于类库的app.config而不是MVC项目的事实有关。

有人有什么建议吗?


当前回答

您的解决方案中是否使用了多个项目?

因为如果你是,你必须检查的web配置是与de .edmx文件在同一个项目中的配置

其他回答

是的,这很愚蠢。使用连接构建器可以避免复制连接字符串。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)

在web中添加Connectoinstrnig。配置文件

<ConnectionStiring> <add name="dbName" Connectionstring=" include provider name too"  ></ConnectionStiring>

当我试图在AutoCAD插件中使用EF时,出现了这个错误。CAD插件从acad.exe.config文件中获取连接字符串。将上面提到的连接字符串添加到acad配置文件中,它就可以工作了。

功劳归于诺曼。来自adn网的袁。

我通过没有将项目设置为启动来得到这一点,正如另一个答案所示。我对此的贡献-当做Add-Migrations和Update-Database时,在Nuget Package Manager Console中指定启动项目作为命令的一部分(不包括'['或']'字符,这只是为了告诉你你需要将位于那里的文本更改为你的项目名称):

Enable-Migrations Add-Migrations -StartupProject[包含数据上下文类的项目名称] Update-Database -StartupProject[与上面的项目名称相同]

这样就行了。

Add an App.Config file Set the project as startup project. Make sure you add the connection strings after entityFramework section: <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> </configSections> <connectionStrings> <!-- your connection string goes here, after configSection --> </connectionString>