create new controller - error running selected cod

2019-06-24 00:49发布

问题:

I am using Visual Studio Express 2013 for Web (specifically version 12.0.21005.1 REL). This is my first project using VS2013, I've been using VS2012 up until this point.

I am attempting to create a new controller in my asp.net MVC application. I am using Entity Framework 5 with code first (.NET 4.5). I want Visual Studio to create the template for me (you know, a controller with views to read/write/delete etc, rather than write the code myself from scratch).

However, every time I try to create the controller I get the following error message:

Is there some sort of bug in VS 2013? I can't figure out what this means, and restarting VS2013 does not help.

Here are the gory details.... actually it is all very simple since this is a new project with very little code written so far.

My model:

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; }
            } 
    }
}

And in my web.config file I have the following

<add name="EFDbContext"
         connectionString="Data Source=JONSNA\SQLEXP2012WT;Initial Catalog=ProfessionalSiteDb; Integrated Security=True; MultipleActiveResultSets=True"
         providerName="System.Data.SqlClient"/>

within the tags.

Now time to create a controller. I right click on Controllers in the Solution Explorer, and choose to Add a new Controller.

And then

And when I click Add I get

I cant figure out how to get rid of this error. I guess as a workaround I can just type the code myself, but I'd like to know if this is a bug or something I have done wrong. In VS2012 this just worked...

I'd appreciate any help or pointers. Thanks.

回答1:

You don't need the EntityModels class, See below:

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; }
        } 
}

Then when you create a controller, just select the Student or Enrollment for the Model class.