Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Entity Framework Core migrations require .NET Core 2.x?

In my .NET5.0 project the PostgreSQL Nuget package is installed (Npgsql.EntityFrameworkCore.PostgrSQL (5.0.2)) and its dependency of EF Core (Microsoft.EntityFrameworkCore (5.0.2)).

I'm trying to create a migration using this command:

dotnet ef migrations add InitialCreate

However, the migration fails because I'm requested to install .NET Core 2.0.0 - following the recommended download link I'm told that it's no longer available and that I would likely want to use 5.0. How the heck can I run the migration?

This is what the output looks:

Build started...
Build succeeded.
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '2.0.0' was not found.
  - The following frameworks were found:
      3.1.11 at [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
      3.1.12 at [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
      3.1.13 at [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
      5.0.3 at [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
      5.0.4 at [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=2.0.0&arch=x64&rid=osx.11.0-x64
like image 269
Krumelur Avatar asked Mar 13 '21 19:03

Krumelur


People also ask

How do EF core migrations work?

EF Core compares the current model against a snapshot of the old model to determine the differences, and generates migration source files; the files can be tracked in your project's source control like any other source file. Once a new migration has been generated, it can be applied to a database in various ways.

Why we use migration in asp net core?

Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.

How do I run a migration in EntityFramework?

Open Microsoft Visual Studio. Set the Console Application name as CodeFirstMigration. Click OK. Add the EntityFramework NuGet package. Run the Install-Package EntityFramework command.

Should I use EF6 or EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

What is Entity Framework Core migration?

Migration in Entity Framework Core Migration is a way to keep the database schema in sync with the EF Core model by preserving data. As per the above figure, EF Core API builds the EF Core model from the domain (entity) classes and EF Core migrations will create or update the database schema based on the EF Core model.

Can I use EF Core with ASP NET Core?

If you want to use EF Core in an application based on ASP.NET Core, we recommend that first you upgrade your application to ASP.NET Core 2.2. In general, the best way to use EF Core in an application is to install the corresponding NuGet package for the provider your application will use.

How do I add my first migration to EF Core?

You're now ready to add your first migration! Instruct EF Core to create a migration named InitialCreate: EF Core will create a directory called Migrations in your project, and generate some files. It's a good idea to inspect what exactly EF Core generated - and possibly amend it - but we'll skip over that for now.

Should I migrate my application to NET Core or NET Framework?

With .Net Framework your application relies on what is installed on the machine you deploy to. Working with .Net Core is not that different from .Net Framework, in fact when I have solutions open in both frameworks I rarely think of which I am currently working in. I would suggest migrating applications that are easy to migrate to .Net Core first.


2 Answers

Make sure you have <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.5" /> in your csproj. Also try updating the dotnet-ef tool: dotnet tool update --global dotnet-ef.

I performed these actions and the issue went away.

like image 142
Mavvie Avatar answered Oct 26 '22 11:10

Mavvie


This can occur if you run the command in a class library. You can point to the startup-project when running the command:

dotnet ef migrations add InitialCreate --startup-project <PathToProject>
like image 28
ErikR Avatar answered Oct 26 '22 13:10

ErikR