create new controller - error running selected code generator

asp.net-mvc asp.net-mvc-5 entity-framework-6 visual-studio-2013

Question

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:

enter image description here

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.

enter image description here

And then

enter image description here

And when I click Add I get

enter image description here

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.

Accepted Answer

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.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why