4

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.

1
  • It is better to rebuild your project before adding controller to avoid this error. I ended here because there was a syntax error and I did not rebuild and simply tried to add the controller. Commented Apr 19, 2014 at 9:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, I feel like an idiot! Why the heck are my classes wrapped within another class ?!?! I think this happened because I was copying/pasting the code from one project to another and didn't bother to actually check the syntax. Serves me right... Thanks Lin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.