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

有些话、适合烂在心里 提交于 2021-01-01 04:26:26

问题


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

回答1:


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.




回答2:


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.




回答3:


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




回答4:


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 :)




回答5:


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.




回答6:


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



回答7:


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.



来源:https://stackoverflow.com/questions/62763225/i-get-an-error-when-i-add-migration-using-entity-framework-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!