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

11

79

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

Timbrel answered 4/1, 2022 at 15:8 Comment(10)
Nobody else has this problem?Timbrel
I have exactly the same problem.Giaimo
I have the same problemSouvaine
I have the very same problem. Same error messages, and the solutions below don't help.Rhizobium
I've the same problem. It's very annoying when following Microsoft Documentation and it throws up stupid errors like this. It means that they don't test that the steps work!Rosemare
I am having exact same issue. I want to use windows authentication only. and .net core 6.0Lesslie
Exact same error - I hate using dotnet6 or it's ilk. While the entity framework shows in the item list it uses the DotNet Framework, and the two cannot reference each other. If you want to use the entity framework with dotnet6 (otherwise called DotNet CORE) you have to use EF Core. Welcome to the command line. Why does it seem like we are going backwards? We have gone from drag and dropping a datalayer back to using command line arguments. PS. Look up EF Core Power Tools.Vallecula
Read on below. The problem is in the way you add the Entity Framework.Timbrel
Sometimes many people face this error - "instance failure while scaffold-dbcontext", The solution is - always use data source like this - Data Source=.\SQLCUSTOM. #52184459Ansilma
same problem here. But I'm trying to do this in WPF projectEmpyema
S
63

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 

Afted 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.

Souvaine answered 21/3, 2022 at 22:51 Comment(8)
How do you update the dbase after the initial scaffolding?Chericheria
@Chericheria I hope this helps https://mcmap.net/q/263142/-update-records-using-linqSouvaine
Sorry @kearl I didn't understood at first, maybe you were asking for this: https://mcmap.net/q/263143/-update-entity-class-in-asp-net-core-entity-frameworkSouvaine
The second link was what I needed (ei -force) thxChericheria
For updating the models, run the same command and at the end of it add -ForceTerrigenous
To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see go.microsoft.com/fwlink/?LinkId=723263. Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process.Demulsify
Just for anyone wanting to use a trusted connection, I used: PM> Scaffold-DbContext "Server=HOST;Database=DB;Trusted_Connection=True;TrustServerCertificate=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -ForcePostaxial
Thanks . Worked for me but need to add TrustServerCertificate=True.Satirist
E
23

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

Epilimnion answered 27/1, 2022 at 8:56 Comment(4)
This should be marked as answerCoating
This creates console application for .Net 4.8 - which do not answers how to do it in .Net 6.Beatification
Hi @Karel Kral You can change a target framework to .Net 6Outage
This answer has saved me 2 days of misery! All I needed was to create a data object for doing my rdlc reports. I just missed the part of selecting WinForms (.Net Framework)Lamina
T
9

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.

Timbrel answered 7/1, 2022 at 13:41 Comment(2)
It -probably- has nothing to do with .NET 6. I tried on .NET 5 and the same issue's there as well.Soothsayer
The link mentioned is about EF Core not EF 6.Beatification
T
9

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.

Terrigenous answered 29/10, 2022 at 14:24 Comment(4)
I believe this is the most appropriate answer, because: 1) it leverages latest Microsoft technologies, versions etc.; 2) it does not make you to downgrade to old .net versions or to use different app templates just for the sake of putting EF in.; @Terrigenous thank you very much.Mononuclear
definitely best. I don't want to scaffold a complete list of the tables in a system. Also the DB team doesn't want me using code-first. Still going to miss the designer.Koenig
This is the method to use in 2024.Monition
The excellent explanation above refers to the 'reverse Engineering' optionHallmark
T
8

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

Toolis answered 13/6, 2022 at 21:22 Comment(0)
A
6

.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.

Adipose answered 26/2, 2022 at 1:31 Comment(0)
S
2

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

Scallion answered 5/1, 2022 at 16:33 Comment(0)
C
0

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

Corporeal answered 24/1, 2024 at 19:0 Comment(0)
A
-1

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

Anthesis answered 9/1, 2022 at 11:27 Comment(0)
A
-4

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.

Alpenstock answered 9/7, 2022 at 15:44 Comment(1)
This is not the right recommendation for my question. Take a look at the answer chosen below.Timbrel
S
-5

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

EF 5

Sleepyhead answered 24/12, 2022 at 4:42 Comment(3)
Are you serious? EF5? In a .Net core 6 project? I think you're confusing Entity Framework and Entity Framework core. Of course they're not going to replace EF6 by EF-core 5, if that's what you mean.Lucic
Yes, you are rightSleepyhead
So maybe it's a good idea to remove the answer?Lucic

© 2022 - 2025 — McMap. All rights reserved.