安装 Microsoft.EntityFrameworkCore、版本=5.0.0.0 后无法搭建 DBContext

Cannot Scaffold DBContext after installing Microsoft.EntityFrameworkCore, Version=5.0.0.0

所以我最近安装了 Entity Framework Core 5.0 Preview 3 因为我需要使用 Filtered Include。

来源:https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-5-0-preview-3/

现在我不得不在我的数据库中更改一个 table,自然地,我在 PackagaManagerConsole 中执行了这个命令:

Scaffold-DbContext "Server=<Server Name>;Initial Catalog=<Database Name>;User ID=<user id>;Password=<password>;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entity -Project <project Name> -force

通常它工作正常并且 DBContext 已更新。 但现在我收到以下异常:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
     ---> System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Internal.SemanticVersionComparer' from assembly 'Microsoft.EntityFrameworkCore, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
       at Microsoft.EntityFrameworkCore.Design.OperationExecutor..ctor(IOperationReportHandler reportHandler, IDictionary args)
       --- End of inner exception stack trace ---
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
       at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
       at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
       at System.Activator.CreateInstance(Type type, Object[] args)
       at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language)
       at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor()
       at Microsoft.EntityFrameworkCore.Tools.Commands.DbContextScaffoldCommand.Execute()
       at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0()
       at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
       at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

我尝试将这些包更新到最新版本:

Microsoft.EntityFrameworkCore ---> 3.1.8

Microsoft.EntityFrameworkCore.Design ---> 3.1.8

Microsoft.EntityFrameworkCore.Relational ---> 3.1.8

Microsoft.EntityFrameworkCore.SQLServer ---> 3.1.8

Microsoft.EntityFrameworkCore.Tools ---> 3.1.8

但我仍然遇到异常。 我应该怎么办?我喜欢 Filtered Include,所以我不想卸载最新的 efcore 预览版。

所以我回去阅读了我在问题中引用的文章,并尝试使用 开发人员命令提示符 而不是 程序包管理器控制台 搭建 DBcontext。 我执行了这个命令:

 `dotnet ef dbcontext scaffold "server=<servername>;database=<database>;user=<user>;password=<password>" Microsoft.EntityFrameworkCore.SqlServ
er --project  "DataAccess" --output-dir "Models" --force`

而且它没有问题。 但我还是不明白为什么会这样。