C# 添加控制器错误无法检索元数据

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

Add controller error unable to retrieve metadata

c#asp.net.netasp.net-mvcvisual-studio

提问by Majid

I want to add controller in my MVC 4 application in VS2012 as this image:

我想在 VS2012 的 MVC 4 应用程序中添加控制器,如下图:

add controller

添加控制器

Model:

模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MvcDemo.Models
{
    public class MovieDB
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Director { get; set; }
        public DateTime Date { get; set; }
    }
    public class MovieDBContext : DbContext
    {
        public DbSet<MovieDB> Movies { get; set; }
    }
}

Connection strings:

连接字符串:

<connectionStrings>

<add name="DefaultConnection" 
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcDemo-20130315191956;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcDemo-20130315191956.mdf"
providerName="System.Data.SqlClient" />

<add name="MovieDBContext"
connectionString="Data Source=|DataDirectory|\Movies.sdf"
providerName="System.Data.SqlServerCe.4.0"/>

</connectionStrings>

After clicking "add", this error occurs:

点击“添加”后,出现这个错误:

unable to retrieve metadata for 'MvcDDemo.Models.MovieDB'.Using the
same DbCompiledModel to create contexts against different type of
database servers is not supported.instead,create a 
separate DbCompiledModel for each type of server being used.

Any suggestion?

有什么建议吗?

采纳答案by Majid

In Web.config, set second providerNamesame as first providerName, and after creating controller, undo that!

在 中Web.config,将 second 设置providerName为与 first 相同providerName,在创建控制器后,撤消它!

from:here

来自:这里

回答by carlosCeron

Change providerName="System.Data.SqlServerCe.4.0to providerName="System.Data.SqlClientto fix this error.

更改 providerName="System.Data.SqlServerCe.4.0providerName="System.Data.SqlClient以修复此错误。

回答by Kateregga John Baptist

In web.config, delete the AttachDBFilename=|DataDirectory|****.mdfline.

在 中web.config,删除该AttachDBFilename=|DataDirectory|****.mdf行。

回答by HPP

User following for providerName = "System.Data.SqlServerCe.4.0"

用户关注 providerName = "System.Data.SqlServerCe.4.0"

<add name="MovieDBContext" 
connectionString="Data Source=|DataDirectory|\Movies.sdf"
providerName="System.Data.SqlServerCe.4.0" />

回答by Haitham Sweilem

My post may help in case someone has the same problem.

如果有人遇到同样的问题,我的帖子可能会有所帮助。

I tried experimenting with EF6 and EF5 in the same project, and I noticed that the <entityFramework> tag was messed up, and got the same problem above.

我尝试在同一个项目中对 EF6 和 EF5 进行试验,我注意到 <entityFramework> 标记搞砸了,并且遇到了上述相同的问题。

Here is what I did, and it solved the problem:

这是我所做的,它解决了问题:

  1. I removed the <entityFramework> tag from Web.config
  2. Uninstalled entity framework from my MVC app using nuget
  3. Installed entity framework to my MVC app again
  1. 我从 Web.config 中删除了 <entityFramework> 标签
  2. 使用 nuget 从我的 MVC 应用程序卸载实体框架
  3. 再次将实体框架安装到我的 MVC 应用程序

After removing the <entityFramework> tag and re-installing EF5 it was fixed, and I can scaffold my controllers again.

删除 <entityFramework> 标记并重新安装 EF5 后,它已修复,我可以再次搭建控制器。

回答by marak

I had the same issue and below is the change which fixed the issue for me. Had to change localhost to . And Added Initial catalog=Movies.

我有同样的问题,下面是为我解决问题的更改。必须将 localhost 更改为 . 并添加了初始目录=电影。

Before

<add name="MovieDBContext" connectionString="Data Source= localhost\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MoviesDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

After (fix)

之后(修复)

<add name="MovieDBContext" connectionString="Data Source= .\SQLEXPRESS;Initial Catalog=Movies; AttachDbFilename=|DataDirectory|\MoviesDB.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>

Reference:

参考:

ASP .Net MVC 4, Invalid value for key 'attachdbfilename'

ASP .Net MVC 4,键“attachdbfilename”的值无效

回答by Scuba Steve

Don't forget to clean and rebuild before you try to scaffold! That was my mistake.

在尝试搭建脚手架之前,不要忘记清洁和重建!那是我的错误。