How to troubleshoot dotnet EF tool? How to solve migration problems?
First of all, let's do a global installation process in order to access the dotnet ef tool from anywhere. Thus, let's be able to access dotnet ef cli commands from anywhere, such as Powershell, terminal, package manager console, etc.
We can do our installation and update processes as follows.
dotnet tool install --global dotnet-ef
dotnet tool update --global dotnet-ef
Let's come to the directory where our context is and run the following command.
dotnet add package Microsoft.EntityFrameworkCore.Design
If you have performed all these processes correctly, you can see the horn of our unicorn as follows.
dotnet ef
_/\__
---==/ \\
___ ___ |. \|\
| __|| __| | ) \\\
| _| | _| \_/ | //|\\
|___||_| / \\\/\\
Let's take comments to friends who cannot see the horn, or you can go and comments the documents. Entity Framework Core
If you can't run classic non-migration commands on the console, let's solve these problems one by one.
dotnet ef migrations add x
I recommend that those who cannot run this command repeat the above procedure and review the document.
Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
To solve this error, edit the sln file of the project you run the command and check if the following packages are installed.
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
Unable to create an object of type 'YourDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
The source of this error is this. There is an error in Design Time caused by your inability to access the configurations you made in Startup.cs where yourDbContext is located. For this, we need to create a Factory class and add it to the project from which the error originated. By using this class that we have created, dotnet ef tool will be able to easily find and read the database connection address and configurations.
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
namespace Project.Data
{
public class YourDbContextFactory : IDesignTimeDbContextFactory<YourDbContext>
{
/// <summary>
/// You can use dotnet ef cli commands with this for different ides.
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public YourDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<YourDbContext>();
optionsBuilder.UseMySql("server=127.0.0.1;database=test_db;user=root;password=123456",
new MySqlServerVersion(new Version(10, 1, 40)),
mySqlOptions => mySqlOptions.CharSetBehavior(CharSetBehavior.NeverAppend));
return new YourDbContext(optionsBuilder.Options);
}
}
}
Here I have configured for MySQL. You can do this in MSSQL as you did in Startup.cs.
.NET 5.0.103 MySQL problem
In Dotnet 5, the standard MySQL package included in EntityFrameworkCore does not work properly. We replaced this package with Pomelo.EntityFrameworkCore.MySql preview because it caused problems in migration processes. When a newer version of this package is available, I recommend using the non-preview version, since I have no alternative solution for this, we will continue this way.
Install-Package Pomelo.EntityFrameworkCore.MySql -Version 5.0.0-alpha.2
We can help if you add your other problems to the comment .. Have a nice working day..