【问题标题】:Cannot Scaffold DBContext after installing Microsoft.EntityFrameworkCore, Version=5.0.0.0安装 Microsoft.EntityFrameworkCore,版本 = 5.0.0.0 后无法搭建 DBContext
【发布时间】:2020-12-31 21:46:49
【问题描述】:

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

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

现在我不得不更改数据库中的一个表,自然而然地,我在 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 预览版。

【问题讨论】:

    标签: asp.net-core entity-framework-core dbcontext


    【解决方案1】:

    所以我回去阅读了我在问题中引用的文章,并尝试使用 developer command prompt 而不是 package manager console 来搭建 DBcontext。 我执行了这个命令:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 2019-04-24
      相关资源
      最近更新 更多