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

Viewed 22801

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; }
}
9 Answers

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.

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.

I am adding to the answer of [mohammed-abdelwahab][1] Below packages need to be of latest update:

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

For that right-click on the project --> Manage Nuget Packages --> Click on updates and just update all one by one or add if not exists as below

 PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
 PM> Install-Package Microsoft.EntityFrameworkCore.Tools
 PM> Add-Migration InitialMigration

Good Luck :)

Maybe you're trying to mix different versions. Make sure that all versions are aligned.

Try changing your SqlServer to the latest version(5.0.0), I was running to exactly this same error, once I updated to the latest version it worked well.

I ran into the same problem when doing an old tutorial. The problem was I installed a package with the "-pre" clause.

Use the below commands:

  1. Enlist installed packages in your project-
    PM> Get-Package -ProjectName <YourProjectName>

Output:

Id                                  Versions                                 ProjectName                     
--                                  --------                                 -----------                     
Microsoft.EntityFrameworkCore.Tools {5.0.0-rc.2.20475.6}                     FirstEFCoreProject              
Microsoft.EntityFrameworkCore.Sq... {3.1.9}                                  FirstEFCoreProject   
  1. Uninstall the unstable package -
    PM> Uninstall-Package Microsoft.EntityFrameworkCore.Tools
  1. Reinstall the package (without the -pre) -
    PM> Install-Package Microsoft.EntityFrameworkCore.Tools
  1. And then add your migration again -
    PM> Add-Migration InitialMigration

I also just had this issue. It turns out that I accidentally had both Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Sqlite installed.

I really just needed Sqlite, so I deleted the reference to SqlServer. I then had to fix a couple of places that referenced SqlServer, instead of Sqlite, I'd missed and now the error is gone.

Fixed after running in NuGet these 2 lines

Add-Migration Initial

and then

Update-Database

.NET Core 6.0.8 Version of Microsoft.EntityFrameworkCore should be Same for all Packages

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

Error comes with this

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0-preview.7.22376.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.8">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.8" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>
Related