I get error when trying to add MVC controller with views using Entity Framework in Visual Studio 2022

56 Views Asked by At

I'm building an ASP.NET Core 6 MVC project as an assignment from school. For this project I have previously added empty controllers and razor views separately without any problems. Now as part of the class, I have to add an "MVC controller with views, using Entity Framework".

When I try adding this controller, I get an error

There was an error running the selected code generator: 'Package restore failed. Rolling back package changes for 'project name'

I'm building the project in .NET 6.0 and added following packages:

  • Microsoft.EntityFrameworkCore.Design (6.0.28)
  • Microsoft.EntityFrameworkCore.SqlServer (6.0.28)
  • Microsoft.EntityFrameworkCore.Tools (6.0.28)
  • Microsoft.VisualStudio.Web.CodeGeneration.Design (6.0.16)
  • Microsoft.VisualStudio.Web.CodeGeneration.Design (6.0.16)

I've tried rebuilding and cleaning the project but I keep getting the same error message.

The controller is for the data from DB table TeamPlayers and the table exists in my LocalDb already as well.

Overview of DB, packages and models

Error message

I added some screenshots to show he error message and to give an overview of the packages and the models I have created in the project.

I've been looking into this problem for a few days now I usually this problem occurs if the entity framework packages aren't the same versions but in my case they all are.

I have tried rebuilding the project from scratch again and make sure that all my packages are the same version. I've also tried cleaning and rebuilding the project in VS 2022.

I hope that someone can help me to find out what is causing this issue or if someone already has a fix for it as i cant find anything online.

1

There are 1 best solutions below

0
Yuning Duan On

You can try these following methods:

1 : Add the online reference: Tools > nuget package manager > package manager settings > Package Sources

2 : varify package source, Add source: https://api.nuget.org/v3/index.json

3 : clear nuget cache and rebuild project nuget locals all-clear

4 : try to lower version and use the same version, downloaded and installed the same version packages

5 : check your solution if you have mutiple projects If you have multiple projects included in your solution you need to make sure that they reference the same metapackage as if there is a version mismatch between projects

If the error still exists, can you share a simple and reproducible example demo so that the problem can be better found.