'How to install Entity Framework in a .Net Framework v4.6.1 application using Visual Studio 2017

My first steps in C# are horrible.

Now I'm trying to install Entity Framework. For doing that, I just go to the NuGet package manager, I type "Entity" and try to install the first entry, the "Microsoft.EntityFrameworkCore", version v5.0.10.

That fails with following error message:

Could not install package 'Microsoft.EntityFrameworkCore 5.0.10'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

How is this possible? .NetFramework, version v4.6.1 is the default in my company, I just clicked on the first NuGet package in my search result and Visual Studio 2017 is, as far as I know, a basic IDE.

In top of that, there is no direction where to go: the error message just says "not compatible" but no mentioning of .NetFramework version being too old or too recent, the .Net Framework being too old or too recent, ...

The error message mentions "to contact the package author", but when I visit the "Project URL" of the Entity Framework (https://docs.microsoft.com/en-us/ef/core/), that website does not even mention .NetFramework.

Does anybody know the simplest way to solve this issue? (I believe installing another version of Entity framework would be advised)

Thanks in advance



Solution 1:[1]

I've confirmed that in VS2017 Nuget Package Manager the first entry is "Microsoft.EntityFrameworkCore" and it is wrongly indicating "Latest Stable 5.0.10" as the applicable version when searching for "Entity" while using references for a .Net 4.6 project. (NuGet should be suggesting based on suitable target dependencies)

As Richard pointed out You can use EF Core within a .Net Framework project, but the limit is EF Core 3.1 as .Net Framework is only supported up to .Net Standard 2.0. If you're using the package manager UI in VS You will want to use the drop-down for "Version:" to select "3.1.19" as this is the last version that will work with .Net Framework 4.x.

I would recommend using EF6 for .Net Framework projects rather than EF Core 3.1 as 3.1 is still missing features found in EF6. Searching for "EntityFramework" will locate "EntityFramework" which is EF6. (Odd that it doesn't seem to come up readily under the search term "Entity")

Note that if you are targeting .Net Framework 4.6.1 the end of support for this version is April 2022. You should strongly consider updating the target to 4.8, or at a minimum 4.6.2.

Solution 2:[2]

Despite @MarcGravell's comment EF Core works fine in a .NET Framework project. But unfortunately, v5 only works in frameworks which support .NET Standard 2.1, which excludes any version of .NET Framework.

.NET Standard | Microsoft Docs

For a .NET Framework project, the latest version of EF Core you can use is v3.1.19. This supports .NET Standard 2.0, which technically includes .NET Framework 4.6.1 and later.

Install-Package Microsoft.EntityFrameworkCore -Version 3.1.19

NB: As mentioned in the MS docs, there are some issues consuming .NET Standard 2.0 libraries from .NET Framework 4.6.1 applications. In particular, you will probably end up pulling in a large number of support packages. It would be better if you could upgrade your application to at least .NET Framework 4.7.2, which doesn't have these issues.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Steve Py
Solution 2 Richard Deeming