asp.net-mvc – 创建新控制器 – 运行所选代码生成器时出错

我正在使用Visual Studio Express 2013 for Web(特别是版本12.0.21005.1 REL).这是我使用VS2013的第一个项目,到目前为止我一直在使用VS2012.

我试图在我的asp.net MVC应用程序中创建一个新的控制器.我首先使用Entity Framework 5代码(.NET 4.5).我希望Visual Studio为我创建模板(你知道,一个带有读/写/删除等视图的控制器,而不是自己编写代码).

但是,每次我尝试创建控制器时,都会收到以下错误消息:

VS 2013中是否存在某种错误?我无法弄清楚这意味着什么,重启VS2013没有帮助.

以下是血腥的细节….实际上它非常简单,因为这是一个迄今为止编写的代码很少的新项目.

我的模特:

namespace ProfessionalSite.Models
{
    public class EntityModels
    {

        public class Student
        {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }

            public virtual ICollection<Enrollment> Enrollments { get; set; }
        }


        public class Enrollment
        {
            public int ID { get; set; }
            public string EnrollmentName { get; set; }
            public string Credits { get; set; }
        }


        // Create the class that inherits from DbContext
        // The name of this class is also used as the connection string in web.config 
        public class EfdbContext : DbContext
        {
            public DbSet<Student> Students { get; set; }
            public DbSet<Enrollment> Enrollments { get; set; }
            } 
    }
}

在我的web.config文件中,我有以下内容

<add name="EfdbContext"
         connectionString="Data Source=JONSNA\sqlEXP2012WT;Initial Catalog=ProfessionalSiteDb; Integrated Security=True; MultipleActiveResultSets=True"
         providerName="System.Data.sqlClient"/>

标签内.

现在是时候创建一个控制器了.我在Solution Explorer中右键单击Controllers,然后选择Add a new Controller.

然后

当我点击Add I get

我无法弄清楚如何摆脱这个错误.我想作为一种解决方法,我可以自己输入代码,但我想知道这是一个错误还是我做错了什么.在VS2012中,这刚刚起作用……

我很感激任何帮助或指点.谢谢.

解决方法

您不需要EntityModels类,请参见下文:

namespace ProfessionalSite.Models
{
        public class Student
        {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }
            public virtual ICollection<Enrollment> Enrollments { get; set; }
        }

        public class Enrollment
        {
            public int ID { get; set; }
            public string EnrollmentName { get; set; }
            public string Credits { get; set; }
        }

        // Create the class that inherits from DbContext
        // The name of this class is also used as the connection string in web.config 
        public class EfdbContext : DbContext
        {
            public DbSet<Student> Students { get; set; }
            public DbSet<Enrollment> Enrollments { get; set; }
        } 
}

然后,在创建控制器时,只需选择Model类的Student或Enrollment.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....