我使用实体框架和ASP。NET MVC 4构建应用程序
我的解决方案分为两个项目;
一个类库,其中包括我的数据模型(.edmx)文件和一些自定义接口 引用上面类库的“容器”MVC项目
我的问题是,当我试图使用'MyEntites' DbContext时,我得到以下错误:
中找不到名为“MyEntities”的连接字符串 应用程序配置文件。
我猜这个问题与连接字符串位于类库的app.config而不是MVC项目的事实有关。
有人有什么建议吗?
我使用实体框架和ASP。NET MVC 4构建应用程序
我的解决方案分为两个项目;
一个类库,其中包括我的数据模型(.edmx)文件和一些自定义接口 引用上面类库的“容器”MVC项目
我的问题是,当我试图使用'MyEntites' DbContext时,我得到以下错误:
中找不到名为“MyEntities”的连接字符串 应用程序配置文件。
我猜这个问题与连接字符串位于类库的app.config而不是MVC项目的事实有关。
有人有什么建议吗?
当前回答
我刚刚发现的解决这个问题的最好方法是临时将该项目(很可能是一个类库)设置为启动项目。这将强制包管理器控制台使用该项目作为它的配置源。这样设置的部分原因是配置文件通常遵循的自顶向下模型。经验法则是,最接近客户端的项目(例如MVC应用程序)是web。将使用的Config或app.config。
其他回答
当我试图在AutoCAD插件中使用EF时,出现了这个错误。CAD插件从acad.exe.config文件中获取连接字符串。将上面提到的连接字符串添加到acad配置文件中,它就可以工作了。
功劳归于诺曼。来自adn网的袁。
确保你已经在启动项目的ROOT web.config中放置了连接字符串。
I know I'm kinda stating the obvious here, but it happened to me too - though I already HAD the connection string in my MVC project's Web.Config (the .edmx file was placed at a different, class library project) and I couldn't figure out why I keep getting an exception... Long story short, I copied the connection string to the Views\Web.Config by mistake, in a strange combination of tiredness and not-scrolling-to-the-bottom-of-the-solution-explorer scenario. Yeah, these things happen to veteran developers as well :)
我也遇到过同样的问题。我错过了把连接字符串到启动项目,因为我正在从其他层执行数据访问操作。同样,如果你的启动项目中没有app.config,那么添加app.config文件,然后向该配置文件添加连接字符串。
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>
我刚刚发现的解决这个问题的最好方法是临时将该项目(很可能是一个类库)设置为启动项目。这将强制包管理器控制台使用该项目作为它的配置源。这样设置的部分原因是配置文件通常遵循的自顶向下模型。经验法则是,最接近客户端的项目(例如MVC应用程序)是web。将使用的Config或app.config。