asp.net-mvc MVC“添加控制器”是“无法检索元数据...配置系统无法初始化”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8238811/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:36:12  来源:igfitidea点击:

MVC "Add Controller" is "Unable to Retrieve Metadata...Config System Failed to Initialize"

asp.net-mvcvisual-studioentity-framework

提问by Brent Arias

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:

从头开始,我用两个项目创建了一个新解决方案:一个是 MVC 3,另一个是支持 EF 4.2 的项目。整个事情构建成功。在 MVC 项目中,我打开“添加控制器”对话框,让它根据我从支持的 EF 项目中选择的上下文和模型生成代码。“添加控制器”对话框失败并显示以下消息:

Unable to retrieve metadata for 'MyModelClass'. Configuration system failed to initialize.

无法检索“MyModelClass”的元数据。配置系统初始化失败。

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:

我注意到“添加控制器”对话框实际上是在尝试从其 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=&quot;
        data source=.;
        initial catalog=Monsters2;
        integrated security=True;
        pooling=False;
        multipleactiveresultsets=True;
        App=EntityFramework
      &quot;" 
      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?

连接字符串实际上并没有所有可笑的换行符和缩进——我只是想让它更容易阅读。无论如何,该连接字符串与在其建模的支持 EF 项目中使用的连接字符串基本相同。我该如何纠正这种情况,让“添加控制器”对话开心?

回答by user2888722

I had installed EF 6 which added:

我已经安装了 EF 6,其中添加了:

<providers>
    <provider invariantName="System.Data.SqlClient" 
        type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

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 5。卸载 EF 6 并重新安装 EF 5 后,我从配置中删除了 ,然后我能够构建我的新控制器。我通过使用发现了这一点

PM> Update-Database –Verbose

回答by Tom

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)

只需将 EF 模型项目 App.Config 中的连接字符串信息复制到您的并注意不要重复任何部分(例如,entityFramework 部分)

回答by cdiazal

Try this:

尝试这个:

<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" 
/>

I have replaced &quot;with '

我已经替换&quot;'

回答by Brian

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:

我将此添加到我的 Global.Asax 文件中,以便 a) 立即启动数据库和 b) 添加表定义。允许控制器找到表定义:

 System.Data.Entity.Database.SetInitializer<ProjectName.DAL.DBConnectContextName>(new CreateDatabaseIfNotExists<ProjectName.DAL.DBConnectContextName>());

回答by Haris Rehman

I face this issue "Unable to retrieve metadata..." while I was dding a Controller. Solution for this is as follows.

我在添加控制器时遇到了“无法检索元数据...”这个问题。对此的解决方案如下。

  1. In Connection String change provider to "system.data.sqlclient" 2.or Remove the Connection String from the Web.config and Add controller and after adding controller again add the connection String.
  1. 在连接字符串中,将提供程序更改为“system.data.sqlclient” 2. 或从 Web.config 中删除连接字符串并添加控制器,然后在添加控制器后再次添加连接字符串。

It seems this is a bug in MVC. I hope with this you can go ahead.

这似乎是MVC中的一个错误。我希望你能继续前进。

回答by ajw1970

I found thjis helped when I had this problem:

当我遇到这个问题时,我发现 thjis 有帮助:

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

卸载了 EF 的预发布版本并安装了 EF5 去了 Sql Server 对象资源管理器并删除了数据库。重建解决方案使用 PM> Update-Database -Verbose

Perhaps the problem was with pre-release EF or just cleansing the palate was all it needed.

也许问题出在预发布的 EF 上,或者只是清洁味觉就足够了。

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.

我对所有这些都很陌生,但这确实让我使用添加控制器脚手架工具,并选择了“具有读/写操作和评论的 MVC 控制器,使用实体框架”模板。

回答by Mamali

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....

首先你应该检查你的连接字符串必须在你的Web.Config 文件中的ROOT 中!之后检查您的项目是否有另一个连接字符串,如果有另一个连接字符串,请将其替换为您的连接字符串....这解决了我的问题,与您的问题相同....

回答by Jorge Rodrigues dos Santos

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.

我设法通过将 edmx 的代码生成策略更改为 T4、添加 EF5 Db 上下文并替换 *.tt 和 *.Context.tt 文件中的 %edmxInputFile% 来解决我的问题。

回答by TinkerBel

Rename the Model class - worked for me

重命名模型类 - 对我来说有效

回答by Digital3D

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.

在创建 Controller 之前,您确定 Web.Config 文件中有 ConnectionString 吗?有时我们会创建另一个包含实体数据库上下文的项目,但我们忘记在另一个项目内的 Web.Config 中添加相同的 connectionString。

<connectionStrings>
    <add name="Monsters2Entities" ...  />
</connectionStrings>