Paul Sinnema
Paul Sinnema

Reputation: 2792

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

Upvotes: 85

Views: 82878

Answers (11)

Angel Montes de Oca
Angel Montes de Oca

Reputation: 1130

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.

Upvotes: 69

Onwuka Daniel
Onwuka Daniel

Reputation: 164

Hope this is not late. I just encountered the same issue. I solved it by selecting the right project template. You should pick No 2 in the image below not No 1 or any template with the (.net Framework) enter image description here

Upvotes: 0

carloswm85
carloswm85

Reputation: 2416

Notice that as far as October 2022:

EF Core does not support visual designer for DB model and wizard to create the entity and context classes similar to EF 6. So, we need to do reverse engineering using the Scaffold-DbContext command. This reverse engineering command creates entity and context classes (by deriving DbContext) based on the schema of the existing database.

How to do this is explained in some of the answers for this question. And more information can be found here.

BUT

This solution provides the same result than the selected answer. I guess that under the hood, it just run the Scaffold-DbContext command (and more, obviously). I quickly tested both solutions, and they produce the same results.

There's a Visual Studio 2022 extension that helps with this issue: EF Core Power Tools

Get it from Marketplace:

enter image description here

Use from the contextual menu, on top of your target project, on "Solution Explorer":

enter image description here

I'll use it in this database:

enter image description here

And when using it, this is what you'll see:

enter image description here

enter image description here

enter image description here

And this is what you'll get:

enter image description here

Remember to add the necessary dependencies.

Here's a quick tutorial on how to use it. It has advanced features as well. The GitHub repository is here.

Upvotes: 12

Vaibhav Kumar
Vaibhav Kumar

Reputation: 1

The best way is to use EntityFramework 5 instead of EF 6, you would be good to go after that.

EF 5

Upvotes: -5

Kat
Kat

Reputation: 1

You just go to solution .csproj, in VisualStudio:

<PropertyGroup>
   <TargetFramework>net4.0</TargetFramework>
</PropertyGroup>
        
<ItemGroup>
   <PackageReference Include="EntityFramework" Version="4.3.1" />
</ItemGroup>"

and change the TargetFramework. To my project it was 5 and changed to 4, after I install EntityFramework Version=4.3.1.

Upvotes: -4

Don Feto
Don Feto

Reputation: 1514

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)

Scaffold-DbContext {-Connection-string-in-quotations-} Microsoft.EntityFrameWorkCore.SqlServer -outputdir Repository/Models -context {-databasename-}DbContext -contextdir Repository -DataAnnotations -Force

To get connection string Tools > Connect to database > Refresh > select your server or write . > select database
You will find your connection string in the server explorer tab.

> Example

Scaffold-DbContext "Data Source=.;Initial Catalog=EDM;Integrated Security=True" Microsoft.EntityFrameWorkCore.SqlServer -outputdir Repository/Models -context MoDbContext -contextdir Repository -DataAnnotations -Force

You will find a new folder named Repository in your solution.

hint: You can remove DataAnnotations -force if you don't want annotations

Upvotes: 8

Cody Hubbard
Cody Hubbard

Reputation: 79

.Net Core 5 and .Net Core 6 both do not support use of the Entity Data Model Wizard that is launched when using the 'Add New Item' -> 'ADO.NET Entity data Model' window in Visual Studio.

As other have mentioned you may change the target version to .Net Framework 4.8 or similar to enable the use of this wizard.

However I like a different approach. Instead I add a class library to my solution that targets .Net Framework 4.8. Then in that class library I use the 'Add New Item' -> 'ADO.NET Entity data Model' which launches that handy wizard that creates the data models for my tables.

You can then build out this class library to handle all of your EntityFramework actions for that particular database. Or you can install the EntityFramework package and reference the classes from within the original project. Just make sure you remember to add the class library as a reference and dependency to your original project in the solution.

You can really do whatever you want with the classes after you have the wizard generate them for you.

Upvotes: 6

Pavan
Pavan

Reputation: 299

Firstly, you need to choose the correct Type of your project (i.e., when creating the project, choose "Console App (.NET Framework)", not the "Console App". This will solve the above error. I had the same error with Visual Studio 2022 using .NET 6, which got resolved after doing this correction

Screenshot

Upvotes: 25

Senthil_pandi
Senthil_pandi

Reputation: 9

Try to install Dot Net Framework lower versions manually or use a visual studio installer to modify the versions. After creating a project right click and go to properties, where you can change the target version. I changed it to 4.8 and the ADO Entity Model is working fine for me

Screenshot

Upvotes: -1

Paul Sinnema
Paul Sinnema

Reputation: 2792

The problem is that EF for .Net 6.0 only works with commands. A colleague sent me this link:

https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

There the Scaffold-DbContext command is explained and that works just fine.

Upvotes: 8

ErikEJ
ErikEJ

Reputation: 41799

The EF 6 tooling onl works on a .NET Framework project, you must add one to your slution and then copy or link to the generated code. In addition, EDMX files in .NET Core projects are not supported, but there are workarounds

Upvotes: 2

Related Questions