Search code examples
entity-framework-6.net-6.0visual-studio-2022

Adding ADO.Net Entity Framework gives "The project's target framework does not contain Entity Framework runtime assemblies"


I've added a new .Net 6.0 project to my solution in VS2022. Installed the EntityFramework 6.4.4. with install-package entityframework and now try to add a ADO.Net Entity Framework Model to the project. I get an error:

The project's target framework does not contain Entity Framework runtime assemblies. Please review the target framework information on the project's property page.

I've tried adding several other EF packages (which should not be necessary according to the documentation here: https://learn.microsoft.com/en-us/ef/ef6/fundamentals/install). I thought the problem was with my installation but I created a .Net 6.0 console application containing the problem and sent it to a colleague and he got the same message.

Also found this topic here: Adding Entity Framework Model on Visual Studio 2022 but there's no answer there.

Steps to reproduce:

  1. Create a .Net 6.0 Console application.
  2. Install the EF6 package using install-package entityframework from the package manager console window.
  3. Right-click solution and choose 'Add' => 'Add item'.
  4. In the left pane click 'Data'.
  5. Choose 'ADO.Net Entity Framework Model.
  6. Click 'Add'.

enter image description here

The error appears:

enter image description here


Solution

  • I know that almost all the other answers recommends to change the target framework, but some users (including me) needs to use .NET 6.0 instead of .NET Framework, so that solution its not valid for us.

    I was able to create the models by using Paul Sinnema's link and using SQL authentication instead of Windows Auth (in my case):

    You will need to install the following packages from NuGet:

    Microsoft.EntityFrameworkCore.Design
    Microsoft.EntityFrameworkCore.SqlServer
    Microsoft.EntityFrameworkCore.Tools 
    

    After that run the following command in the Package Manager Console: (Tools>Nuget Package Manager>Package Manager Console)

    PM> Scaffold-DbContext "Server=.\LOCAL_SERVER;User ID=YOUR_DB_USER;Password=YOUR_DB_PASSWORD;Database=YOUR_DATABASE;Trusted_Connection=False;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
    

    In case you want to use a trusted connection (from SimonDK comment):

    PM> Scaffold-DbContext "Server=HOST;Database=DB;Trusted_Connection=True;TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
    

    In case you need to update the DB, you can update your models by adding -Force at the end.