MVC“添加控制器” “无法检索元数据...配置系统无法初始化”
我从头开始创建了一个包含两个项目的新解决方案:一个是 MVC 3,另一个是支持 EF 4.2 项目。整个事情构建成功。我从 MVC 项目中打开“添加控制器”对话框,并让它根据我从支持 EF 项目中选择的上下文和模型生成代码。 “添加控制器”对话框失败并显示以下消息:
无法检索“MyModelClass”的元数据。配置系统初始化失败。
我注意到“添加控制器”对话框实际上是在尝试从其 web.config 文件中获取数据库连接字符串。首先,这让我觉得很愚蠢,因为支持的 EF 项目已经有一个带有连接字符串的 app.config。但不管怎样,我能想到的最好的结果是 web.config 中的连接字符串不知何故有问题。它看起来是这样的:
<add name="Monsters2Entities"
connectionString="
metadata=res://*/Monsters.csdl|
res://*/Monsters.ssdl|
res://*/Monsters.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=.;
initial catalog=Monsters2;
integrated security=True;
pooling=False;
multipleactiveresultsets=True;
App=EntityFramework
""
providerName="System.Data.EntityClient"
/>
连接字符串实际上并没有所有可笑的换行符和缩进 - 我只是想让它更容易阅读。不管怎样,该连接字符串与它所建模的支持 EF 项目中使用的连接字符串基本相同。我该如何纠正这种情况,以使“添加控制器”dialgoue 满意?
From scratch, I made a new solution with two projects: one was MVC 3 and the other a supporting EF 4.2 project. The whole thing builds successfully. From the MVC project I open the "Add Controller" dialogue and have it generate code based on the context and model I select from the supporting EF project. The "add controller" dialogue fails with the message:
Unable to retrieve metadata for 'MyModelClass'. Configuration system failed to initialize.
I've noticed that the "add controller" dialogue is actually attempting to fetch the database connection string from its web.config file. First, this strikes me as goofy-ish, since the supporting EF project already has an app.config with the connection string. But never-minding that, the best I can figure is that the connection string in the web.config is bad somehow. This is what it looks like:
<add name="Monsters2Entities"
connectionString="
metadata=res://*/Monsters.csdl|
res://*/Monsters.ssdl|
res://*/Monsters.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=.;
initial catalog=Monsters2;
integrated security=True;
pooling=False;
multipleactiveresultsets=True;
App=EntityFramework
""
providerName="System.Data.EntityClient"
/>
The connection string doesn't actually have all the ridiculous line breaks and indentation - I'm just trying to make it easier to read. Anyway, that connection string is basically identical to the connection string used in the supporting EF project upon which it is modelled. How do I correct the situation, to make the "add controller" dialgoue happy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我已经安装了 EF 6,其中添加了:
在我的配置文件中。我遇到了其他脚手架问题,因此决定回退到 Ef 5。卸载 EF 6 并重新安装 EF 5 后,我从配置中删除了 EF 6,然后我就能够构建新的控制器。我通过使用发现了这一点
I had installed EF 6 which added:
In my config file. I had other scaffolding issues and so decided to fall back to Ef 5. After uninstalling EF 6 and reinstalling EF 5 I deleted the from the config and then I was able to build my new controller. I discovered this by using the
只需将 EF 模型项目 App.Config 中的连接字符串信息复制到您的文件中,并注意不要重复任何部分(例如,entityFramework 部分)
<强>
Just copy the connection string information from your EF model project App.Config into your and watch that you do not repeat any sections (entityFramework section for example)
试试这个:
我已将
"
替换为'
Try this:
I have replaced
"
with'
我将其添加到 Global.Asax 文件中,以便 a) 立即启动数据库,b) 添加表定义。允许控制器找到表定义:
I added this to my Global.Asax file so that a) the database was started immediately and b) the table definition was added. allowing the Controller to find the table definition:
当我遇到这个问题时,我发现 thjis 有帮助:
卸载了 EF 的预发行版本并安装了 EF5
转到 Sql Server 对象资源管理器并删除 db。
重建解决方案
使用PM> Update-Database -Verbose
也许问题出在预发布的 EF 上,或者只是清理一下口味就足够了。
我对所有这些都很陌生,但这确实让我可以使用添加控制器脚手架工具,并选择“具有读/写操作和评论的 MVC 控制器,使用实体框架”模板。
I found thjis helped when I had this problem:
Uninstalled the pre-release version of EF and installed EF5
Went to Sql Server Object Explorer and deleted the db.
Rebuilt the solution
used PM> Update-Database -Verbose
Perhaps the problem was with pre-release EF or just cleansing the palate was all it needed.
I'm quite new to all of these but this did let me use the Add Controller scaffolding tool with the "MVC controller with read/write actions and reviews, using Entity Framework" template selected.
首先你应该检查你的连接字符串必须在你的Web.Config文件的ROOT中!之后检查您的项目是否有另一个连接字符串,如果有另一个连接字符串,请将其替换为您的连接字符串......
这解决了我的问题,与你的问题相同......
First of all you should check your connection string must be in your Web.Config file in the ROOT! of your project after that check if there is another connection string or not if there is another connection string replace it with your connection string....
This solved my problem which was same as your's....
我设法通过将 edmx 的代码生成策略更改为 T4、添加 EF5 Db 上下文并替换 *.tt 和 *.Context.tt 文件中的 %edmxInputFile% 来解决我的问题。
I managed to solve my problem by changing the edmx's code generation strategy to T4, adding a EF5 Db Context and replacing %edmxInputFile% in both the *.tt and *.Context.tt files.
重命名模型类 - 对我有用
Rename the Model class - worked for me
在创建 Controller 之前,您确定 Web.Config 文件中有 ConnectionString 吗?有时我们创建另一个包含实体数据库上下文的项目,但我们忘记在另一个项目内的 Web.Config 中添加相同的连接字符串。
Are you sure to have a ConnectionString in your Web.Config file before to create a Controller ? Sometimes we create another project which contains the Entity DB Context and we forgot to add this same connectionString in the Web.Config inside the another project.
在添加控制器时,我遇到了“无法检索元数据...”这个问题。
解决方案如下。
2.或从 Web.config 中删除连接字符串并添加控制器,然后在添加控制器后再次添加连接字符串。
看来这是 MVC 中的一个错误。
我希望你能继续前进。
I face this issue "Unable to retrieve metadata..." while I was dding a Controller.
Solution for this is as follows.
2.or Remove the Connection String from the Web.config and Add controller and after adding controller again add the connection String.
It seems this is a bug in MVC.
I hope with this you can go ahead.
首先需要安装包实体框架
进入工具>
库包管理器>包管理器控制台> :安装实体框架
first need to install package entity framework
go to tool>
Libray package manager> pakage manager console > :install entity framework