Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get an error when I add migration using Entity Framework Core

I built a console project and use code first to map model to database. When I run the command of Add-Migration InitialMigration, I get an error:

Method 'Create' in type 'Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerSqlTranslatingExpressionVisitorFactory' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=3.1.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.

The DbContext is:

class ActorDbContext : DbContext
{
    public DbSet<Actor> Actors { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            @"Server=(localdb)\mssqllocaldb;Database=ActorDb;"
            + "Trusted_Connection=True;");
    }
}

The entity is:

public class Actor
{
    public int Id { get; set; }
    public String Name { get; set; }
    public int Age { get; set; }
    public bool AcademyWinner { get; set; }
}
like image 201
AuroraWang Avatar asked Jul 06 '20 19:07

AuroraWang


2 Answers

You must include all the following packages to be of same version:

Microsoft.EntityFrameworkCore
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer

I tried it and it run well.

like image 153
Mohammed Abdelwahab Avatar answered Sep 28 '22 11:09

Mohammed Abdelwahab


I just ran into this same issue using VS for Mac. My problem was I had the following versions of packages installed:

  • Microsoft.EntityFrameworkCore.Tools 5.0.0-preview.8.20407.4
  • Microsoft.EntityFrameworkCore.Design 5.0.0-preview.8.20407.4
  • Microsoft.EntityFrameworkCore.SqlServer 3.1.8

Take note of the different versions used. To correct the issue I uninstalled the preview versions of the packages and installed the latest stable versions.

  • Microsoft.EntityFrameworkCore.Tools 3.1.8
  • Microsoft.EntityFrameworkCore.Design 3.1.8
  • Microsoft.EntityFrameworkCore.SqlServer 3.1.8

Again take note of the versions for all 3 packages. Once I had installed the correct version of each package the issue was resolved and my Add-Migration worked.

like image 35
WBuck Avatar answered Sep 28 '22 10:09

WBuck