There was an error running the selected generator

2019-04-09 16:19发布

When I create the Scaffold and add the Model class then I am getting these error "There was an error running the selected generator . try rebuilding the project"

I have three Model class :

1.Department.CS

2.Designation.cs

3.CompanyDBContext.cs

Database : I have two table in database, 1. Department(deptID,deptName,Description) 2. Designation(desgtID,desgName,description)

Objective :- I want to create one view page for these scenario. Like this

Insert Name of Form (TextBox) + Department Name (Dropdown list box) + Designation Name (Dropdown list box)

1.Department.CS

namespace mvcAppraisalSystem.Models
{
  public class Department
  {
    [Key]
    public int deptID { get; set; }
    public string deptName { get; set; }
    public string Description { get; set; }
  }
 }

2.Designation.cs

namespace mvcAppraisalSystem.Models
{
   public class Designation
   {
      [Key]
      public int desgID { get; set; }
      public string desgName { get; set; }
      public string description { get; set; }
   }
 }

3.CompanyDBContext.cs

 namespace mvcAppraisalSystem.Models
 {
   public class CompanyDBContext : DbContext
   {
      public DbSet<CompanyDBContext> Departments { get; set; }

      public DbSet<CompanyDBContext> Designations { get; set; }
   }
 }

7条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-09 16:59

Hope this helps someone. I tried some of the suggestions in SO but in my case, it was something else. I forgot that I had created a partial class where I keep my Data Annotations which had a property that I removed from the model/database. So, after I updated the model from the database, project compiled fine I didn't notice any issue. But, later on when I tried to create a controller with scaffolding, EF started throwing "There was an error running the selected generator. try rebuilding the project". I finally able to resolve the issue after I remembered and removed the property from the partial class.

查看更多
放荡不羁爱自由
3楼-- · 2019-04-09 17:01

This is the problem with the connection string you can change the connection string with your DbContext name like bellow on the file web.config:

<connectionStrings>
  <add name="MovieDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MVCmove-20140616082808.mdf;Initial Catalog=aspnet-MVCmove-20140616082808;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

For the class:

using System;
using System.Data.Entity;
namespace MVCmove.Models
{
  public class Movie
  {
    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string Default1Genre { get; set; }
    public decimal Price { get; set; }
  }
  public class MovieDBContext : DbContext
  {    
    public DbSet<Movie> Movies { get; set; }
  }
}
查看更多
女痞
4楼-- · 2019-04-09 17:01

I built the project as they suggested in the error: "Try rebuilding the project". And then I tried it once again. And the error was gone. The creation of new project did not work in my case me. Running a build did.

查看更多
老娘就宠你
5楼-- · 2019-04-09 17:06

I got this error attempting to build my first Scaffolded item on a newly created project using database first EF.

It's important to Build the Project after adding a new Entity Data Model. Apparently, it's necessary to initialize/configure aspects of the project.

I had read about this previously in a database first tutorial, but had forgotten it. Eventually, I remembered it after digging through related SO issues, including this one.

查看更多
We Are One
6楼-- · 2019-04-09 17:14

No idea if anyone else has had this issue since but restarting Visual Studio removed the error for me in VS2017.

查看更多
干净又极端
7楼-- · 2019-04-09 17:20

I renamed Model, then tried to add controller again, then I renamed Model to first name. After that creating of the controller was successful.

查看更多
登录 后发表回答