本文介绍了MVC4 脚手架添加控制器给出错误“无法检索元数据...";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 RTM 版本的 Windows 8 和 VS 2012 Ultimate.我有一个使用 SqlCe 4.0 和代码优先实体框架模型的 MVC4 项目.

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.

模型很简单:

   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:

无法检索 MyThing 的元数据"

"使用相同的 DbCompiledModel 创建针对不同的上下文不支持的数据库服务器类型.相反,创建一个为正在使用的每种类型的服务器单独的 DbCompiledModel.

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

如何让脚手架发挥作用?

How do I get scaffolding to work?

推荐答案

通过反复试验,我发现了导致错误的代码行(它是 DbContext ctor):

By trial and error I found the line of code (it's the DbContext ctor) that is causing the error:

public class MyThingDb : DbContext
{
    // If I comment this constructor out the scaffolding works
    public MyThingDb()
        : base("DefaultConnection")
    {
    }

    public DbSet<MyThing> Things{ get; set; }
}

WTF?

这篇关于MVC4 脚手架添加控制器给出错误“无法检索元数据...";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 16:31