添加控制器在MVC4不工作 [英] Add Controller in MVC4 not working

查看:96
本文介绍了添加控制器在MVC4不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS 2010 premium。我一直在使用SQLCE 4.0与实体框架模型MVC4项目。

模式是:

 公共类ProjectBuild
    {
       公众诠释ProjectBuildID {获取;设置;}
       公共字符串名称{;设置;}
    }  公共类ProjectBuildContext:的DbContext
     {
       公共DbSet< ProjectBuild>建立{获取;集;}
     }

下面是我的连接字符串:

 添加名称=ProjectBuildContext的connectionString =数据源= | DataDirectory目录| DB.sdf
 的providerName =System.Data.SqlServerCe.4.0

当我尝试创建一个新的控制器内置的脚手架嫌我得到以下错误:


  

无法检索元数据ProjectBuild,使用相同的
  DbCompiledModel打造针对不同类型的数据库上下文
  不支持的服务器。相反,创建一个单独的DbCompiledModel
  对于每个类型的服务器被使用。



解决方案

我试过Fontanka16解决方案,但它没有工作,事实证明,我的DbContext类失踪的默认构造函数定义目标CE数据库。

这是我的步骤总结:


  • 安装了的NuGet包EntityFramework.SqlServerCompact。

  • 添加默认的构造函数对我的DbContext类。

    公共类的 SchoolContext :的DbContext
    {
        公共SchoolContext():基地(学校){}
        ...
    }


  • 我的连接字符串是:

     <添加名称=SchoolContext的connectionString =数据源= | DataDirectory目录| School.sdf的providerName =System.Data.SqlServerCe.4.0/>


然后它的工作。

I'm using VS 2010 Premium. I have a MVC4 project using SqlCe 4.0 with a entity framework model.

Model is:

  public class ProjectBuild
    {
       public int ProjectBuildID {get;set;}
       public string name {get;set;}
    }

  public class ProjectBuildContext:DbContext
     {
       public DbSet<ProjectBuild> builds {get;set;}
     }

Below is my connection string:

 add name="ProjectBuildContext" connectionString="Data Source=|DataDirectory|DB.sdf"
 providerName="System.Data.SqlServerCe.4.0"

When I try to create a new controller with the built in scaffolding too I get the following error:

"Unable to retrieve metadata for ProjectBuild"."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.

解决方案

I tried Fontanka16 solution, but it did not work, it turned out that my DbContext class was missing its default constructor defining the target CE database.

These are my steps summary:

  • Installed the Nuget package EntityFramework.SqlServerCompact.
  • Added the default constructor to my DbContext class.

    public class SchoolContext : DbContext { public SchoolContext() : base("School") { } ... }

  • My connection string is:

    <add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0" />
    

Then it worked.

这篇关于添加控制器在MVC4不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆