How to fix this error when trying to scaffold EF DBContext

Franky Lin 20 Reputation points
2024-10-29T22:49:05.12+00:00

Hi,

I'm trying to scaffold a DBContext using EF in an MVC 4 project. I don't think it matters, but it is not a .NET Core project. I'm getting the following error when I run the scaffold command

Command in CLI:

dotnet ef dbcontext scaffold "Server=.;Database=AdventureWorks2019;Trusted_Connection=True;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -o Models

Error:

C:\GitHub\FM_MVC4\fm_mvc\FM_MVC4.csproj(226,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\8.0.403\Microsoft\VisualStudio\v17.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the expression in the Import declaration "C:\Program Files\dotnet\sdk\8.0.403\Microsoft\VisualStudio\v17.0\WebApplications\Microsoft.WebApplication.targets" is correct, and that the file exists on disk.

Unable to retrieve project metadata. Ensure it's an SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.

I've never seen this kind of error before, and I'm not sure what's going on. My EF was always running fine. Does anyone know how to fix this?

Developer technologies | .NET | Entity Framework Core
Developer technologies | .NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2024-10-30T07:41:44.58+00:00

    Hi,@Franky Lin. Welcome to Microsoft Q&A. 

    Executing your Scaffold command in the CLI requires the project to install Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Design, and Microsoft.EntityFrameworkCore.Tools. However, these packages cannot be installed in .NET Framework projects.

    In a .NET Framework project (ASP NET Web Application), you cannot use the Database-First approach of Entity Framework Core. Instead, you should use the Database-Firstapproach of Entity Framework 6:

    https://learn.microsoft.com/en-us/ef/ef6/modeling/designer/workflows/database-first.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.