You are currently viewing .NET Core 3 and Entity Framework Core Migrations

.NET Core 3 and Entity Framework Core Migrations

I have written a blog post about EF core few months back. Today, I was trying to setup a sample .NET Core 3 application with EF Core.

I found that dotnet ef migration command was not working for me. I referred my blog and found that I followed all the necessary steps.

In this article, lets try to see how can we use dotnet ef migration with .NET Core 3 applications.

The Application

Go to visual studio and create .NET Core 3 MVC web application.

Then I added business and data layer assemblies. There is a separate assembly for entity framework models.

I did all necessary things to setup the entity framework as mentioned in my previous blog article.

You can download the source code of this application from my GitHub repository.

First Error

When I tried to run below command to create the migration, I got the error which is shown in snapshot below.

dotnet ef migrations add First --startup-project ../MyFitnessLog     

The summary of below error is, system could not execute command because the specified command or file was not found. As far as I knew, the dotnet ef commands were available out of the box and we are not required to install anything else. But I was wrong this time.

First Resolution

After some research, I found out that there are some updates in .NET Core 3.

Starting in 3.0, the dotnet ef command-line tool is no longer included in the .NET Core SDK. Before you can execute EF Core migration or scaffolding commands, you’ll have to install this package as either a global or local tool.  Refer this blog for more details about this.

So, I executed below command to install the global tool.

After successful installation, I thought everything is done and now ef migration should work for me. But, it did not help.

dotnet tool install --global dotnet-ef --version 3.1.0

Second Error

I tried below command again to create the migrations:

dotnet-ef migrations add First --startup-project ../MyFitnessLog

It failed again with different error as shown below.

Your startup project ‘MyFitnessLog’ doesn’t reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

Second Resolution

As suggested in the error, I tried to add reference of the nuget package Microsoft.EntityFramework.Core.Design 3.1.0. This reference was added to my web project.

After adding this reference, clean and build the solution.

Why ?

The reason for the change is explained in the docs:

This change allows us to distribute and update dotnet ef as a regular .NET CLI tool on NuGet, consistent with the fact that the EF Core 3.0 is also always distributed as a NuGet package.

In addition, you might need to add the following NuGet packages to your project:

Finally, it is working !

I was able to create the database using below commands to create migration and then create the database.

The database will be created in the SQL Server specified in the connection string of application’s configuration.

dotnet-ef migrations add First --startup-project ../MyFitnessLog
dotnet-ef database update --startup-project ..\MyFitnessLog

I hope you enjoyed this article. Let me know your thoughts.

Leave a ReplyCancel reply

This Post Has 2 Comments