There was an error running the selected generator . try rebuilding the project

随声附和 提交于 2019-12-04 08:31:42
Nasel P Nazeer

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

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.

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.

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.

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

I was getting the same error above on VS2017 also.

My Solution was to unload my project and then edit the csproj file and look for the reference to System.Web.MVC and deleted it, then saved and reloaded the project. I then went to manage NuGet packages and you can then Reinstalled Microsoft.AspNet.Mvc package.

Cleaned and rebuilt the solution and tried to add the controller again and it worked!!

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!