2

I have issues creating a Controller with Scaffolding items. I'm working with Visual Studio 2019 - ASP.NET Core Web Application (with MVC) - ASP.NET Core 3.1

I did the following steps:

  1. I installed from Nuget Packages Manager:
  • Microsoft.EntityFrameworkCore (3.1.3)
  • Microsoft.EntityFrameworkCore.SQLServer (3.1.3)
  • Microsoft.EntityFrameworkCore.Tools (3.1.3)
  1. I made a Model class
  2. I made a DB context class
  3. I added in the ConfigureServices method of Startup class the connection with the DB context and String connection
  4. In appsettings.json I added the String connection to SQLSERVER
  5. right-click on Controllers folder, Add > Controller. Then select ‘MVC Controller with views, using Entity Framework’, selected Model and DB Context but when it's creating the Controller and View the VS showed me an error "Package restore failed. Rolling back package changes for "Name of Project" "

I read other questions related to this, I delete the cache from the Nuget Packages, Rebuild project, change the version of the packages, etc, but the issue still happening.

EzeS
  • 21
  • 1
  • 2

3 Answers3

2

Just wanted to add if anyone is seeing this with the Angular SPA template in VS 2019. I tried all the tricks (manual install/update packages, manually clear cache, restart VS, restart computer...), none of them worked.

What finally ended up working was when I randomly decided to look at the csproj after the scaffolding failed and noticed that the Code Generator (latest, at time of writing 3.1.5) set the version of EFCore.SqlServer back to 3.1.13 after I had manually updated to latest (at time of writing 3.1.14).

Basically, the latest version of the code gen is still referencing the older version of the packages so scaffolding never worked

(auto-upgrade and auto-downgrade of packages was failing)

2

I had mostly version 5.0.9 of nuget packages which were last stable at the moment. However New Scaffolded Item wanted 5.0.8 . So I had to downgrade all the nugets to lower version. After that New Scaffolded Item worked out.

Danil
  • 701
  • 8
  • 7
1

I had a similar problem Scaffolding a Controller with views, using Entity Framework in VS2019 .NET Core 3.1.

Adding the following packages should solve your problem:

  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.VisualStudio.Web.CodeGeneration.Design

you can do it via:

.NET Core CLI

dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.Design

NuGet Package Manager download the package with the appropriate version to your project

Find the packages above with the suited version and add them to your project.

Yonatan Gross
  • 387
  • 5
  • 16
  • Thanks Yonatan for your quick reply. Unfortunately, the issue still happening. Also, i have updated the package's version to 3.1.8 but it wasn't a workaround. Maybe i have to uninstall VS and install again... or change the version of VS – EzeS Dec 07 '20 at 13:09
  • I couldn't find an additional reason for the issue happening to you. If I'll find a solution I will update my answer. It would help if you can add photos of your classes: DbContext, Models, startup and project – Yonatan Gross Dec 07 '20 at 16:07
  • 1
    Yonatan i have resolved the issue. I download again the packages with the last version and also add more using in the StartUp Class, with that everything works well. :) – EzeS Dec 08 '20 at 13:52
  • @EzeS that also does the trick ;) make sure in the future when you update some dependencies that all other are one compatible. – Yonatan Gross Dec 08 '20 at 18:47