Cannot generate a new controller via scoffolding in Entity/C#

87 views Asked by At

I am taking a tutorial on getting started with Entity Framework and C#. I was following the [tutorial][1] and I am getting an error when I tried to create a new scaffolding item. When I select the Model class, data context class and select the controller name, I get the following error message: "There was an error running the selected code generator: 'There was an error getting the type 'ContosoUniversity.Models.Student'. Try rebuilding the project."

I did rebuild the project, twice actually, and I keep getting this error message. Can anybody tell me what might be happening?

These are the steps I took to create the new scaffold item:

  • Right-click the Controllers folder in Solution Explorer, select Add, and then click New Scaffolded Item.

    In the Add Scaffold dialog box, select MVC 5 Controller with views, using Entity Framework, and then choose Add. In the Add Controller dialog box, make the following selections, and then choose Add:

    Model class: Student (ContosoUniversity.Models). (If you don't see this option in the drop-down list, build the project and try again.)

    Data context class: SchoolContext (ContosoUniversity.DAL).

    Controller name: StudentController (not StudentsController).

    Leave the default values for the other fields.

This is how I have the code set up in my Student class file, under Models:

using System;
using System.Collections.Generic;

namespace ContosoUniversity.Models
{
    public class Student
    {
        public int ID { get; set; }
        public string LastName { get; set; }
        public string FirstMidName { get; set; }
        public DateTime EnrollmentDate { get; set; }

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

Finally, this is how I have my connections set up to the localDB that is being used in the tutorial.

> <connectionStrings>
>     <add name="SchoolContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial
> Catalog=ContosoUniversity1;Integrated Security=SSPI;"
> providerName="System.Data.SqlClient"/>   </connectionStrings>
1

There are 1 answers

0
Akshay Phadke On

Change your connection string

From this

<add name="xxx" connectionString="Data Source=xxx;initial catalog=xxx;Persist Security Info=True;User ID=xxxx;Password=xxxx;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

To this

<add name="xxx" connectionString="metadata=res://*/EFMOdel.csdl|res://*/EFMOdel.ssdl|res://*/EFMOdel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxxx;initial catalog=xxxx;persist security info=True;user id=xxxx;password=xxx;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />