asp.net-mvc MVC4 脚手架添加控制器给出错误“无法检索元数据...”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12165185/
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
MVC4 Scaffolding Add Controller gives error "Unable to retrieve metadata..."
提问by Hyman Ukleja
I'm using RTM version of Windows 8 and VS 2012 Ultimate. I have a MVC4 project using SqlCe 4.0 with a code first entity framework model.
我正在使用 Windows 8 和 VS 2012 Ultimate 的 RTM 版本。我有一个 MVC4 项目,使用 SqlCe 4.0 和代码优先实体框架模型。
Model is very simple:
模型很简单:
public class MyThing
{
public int MyThingId { get; set; }
public int UserId { get; set; }
public string Title { get; set; }
public string Address { get; set; }
public string Description { get; set; }
public DateTime Date { get; set; }
}
When I try to create a new controller with the built in scaffolding too I get the following error:
当我也尝试使用内置脚手架创建新控制器时,我收到以下错误:
"Unable to retrieve metadata for MyThing"
"Using the same DbCompiledModel to create contexts against different types of database servers is not supported. Instead, create a separate DbCompiledModel for each type of server being used.
“无法检索 MyThing 的元数据”
“不支持使用相同的 DbCompiledModel 针对不同类型的数据库服务器创建上下文。相反,为正在使用的每种类型的服务器创建单独的 DbCompiledModel。
How do I get scaffolding to work?
我如何让脚手架工作?
采纳答案by Hyman Ukleja
By trial and error I found the line of code (it's the DbContext ctor) that is causing the error:
通过反复试验,我发现了导致错误的代码行(它是 DbContext ctor):
public class MyThingDb : DbContext
{
// If I comment this constructor out the scaffolding works
public MyThingDb()
: base("DefaultConnection")
{
}
public DbSet<MyThing> Things{ get; set; }
}
WTF?
跆拳道?
回答by Henrik
I also stumbled into this symptom while running a tutorial on the subject of building an MVC Music Store application.
在运行有关构建 MVC 音乐商店应用程序的教程时,我也偶然发现了这种症状。
There definitely seem to be a bug within Visual Studio. What seems to trigger this bug is choosing some name, other than the default, used for the connection string.
Visual Studio 中似乎肯定存在错误。似乎触发此错误的是选择用于连接字符串的名称,而不是默认名称。
My thanks goes to user dwaynef on http://forums.asp.net/t/1838396.aspx/1for finding this workaround.
感谢http://forums.asp.net/t/1838396.aspx/1上的用户 dwaynef找到了这个解决方法。
A bit elaborated you need to, temporarily during addition of the new scaffolding controller, change the name of your connection string to 'DefaultConnection' in web.config:
稍微详细说明一下,您需要在添加新的脚手架控制器期间暂时将连接字符串的名称更改为 web.config 中的“DefaultConnection”:
<connectionStrings>
<add name="DefaultConnection" ... />
</connectionStrings>
If you have more than one connection string - make sure only this one is there while performing the action.
如果您有多个连接字符串 - 确保在执行操作时只有这一个。
回答by Mr Nice
Here's my two cents worth. I don't believe your solution actually addresses the real issue. The real fix is to pass the base constructor the database name rather than the connection string name so if your connection string is
这是我的两分钱。我不相信您的解决方案实际上解决了真正的问题。真正的解决方法是将数据库名称而不是连接字符串名称传递给基本构造函数,因此如果您的连接字符串是
<add name="MyContext" connectionString="Data Source=|DataDirectory|MyDatabase.sdf" providerName="System.Data.SqlServerCe.4.0" />
you're context class should be defined as
你的上下文类应该定义为
public class MyContext : DbContext
{
public MyContext() : base("MyDatabase") { }...
Hope this works for you and others as it does for me.
希望这对你和其他人有效,就像对我一样。
回答by SolidSnake
Here is what worked for me:
这是对我有用的:
- Go to connection string in web.config change the following:
providerName="System.Data.SqlClient"
- 转到 web.config 中的连接字符串更改以下内容:
providerName="System.Data.SqlClient"
instead of
代替
providerName="System.Data.SqlServerCe.4.0"
providerName="System.Data.SqlServerCe.4.0"
- Generate your controller.
- Rename providerName back to "
System.Data.SqlServerCe.4.0". - run your project.
- 生成您的控制器。
- 将 providerName 重命名为“
System.Data.SqlServerCe.4.0”。 - 运行你的项目。
回答by Suplanus
Works for me: In the "Add New Scaffolded Item" dialog, i added a new context (plus) with any name (for me "ScaffoldingContext"). Then the scaffolding works. Just rename the context in the Controller.
对我有用:在“添加新的脚手架项目”对话框中,我添加了一个具有任何名称的新上下文(加号)(对我来说是“脚手架上下文”)。然后脚手架工作。只需重命名控制器中的上下文即可。
回答by blin2linkme
Problem may be because of missing
[NotMapped]Attribute
in one of the model class.
问题可能是因为[NotMapped]模型类之一中缺少
属性。
As I missed the attribute and I was cunning my head.
因为我错过了这个属性,所以我很狡猾。
[Display(Name="Logo")]
[DataType(DataType.Upload)]
[NotMapped]
public HttpPostedFileBase Logo { set; get; }
回答by irfandar
This may sometimes be caused by an association property or a foreign key attribute
这有时可能是由关联属性或外键属性引起的
回答by VivekPKumar
After trying different options, the below method resolves the error..
尝试不同的选项后,下面的方法解决了错误..
If the name value in connection string matches with the value passed to the constructor, it works.
如果连接字符串中的名称值与传递给构造函数的值匹配,则它起作用。
public MyThingDb()
: base("name=MyContext")
{
}
回答by Mawuli Sevor
Change "Things" inside
改变里面的“东西”
public
DbSet<MyThing> Things
{ get; set; }
}
to
"Database1" where "Database1" is the name of the database file on disk which appears in your Web.configfile as "Database1.sdf"
到“Database1”,其中“Database1”是磁盘上数据库文件的名称,它在您的Web.config文件中显示为“Database1.sdf”
回答by guest
The solution that worked for me is to pass the same database name as in your connection string to the base constactor of your dbContext class.
对我有用的解决方案是将与连接字符串中相同的数据库名称传递给 dbContext 类的基本构造函数。

