【问题标题】:Why am I encountering an error when I run "Add-Migration InitialCreate" in .NET?为什么我在 .NET 中运行“Add-Migration InitialCreate”时遇到错误?
【发布时间】:2021-03-06 04:25:32
【问题描述】:

我有一个使用 Asp.net MVC 和 .NET core 3.1 构建的应用程序。我已经创建了模型、数据库上下文类和控制器。当我尝试在包管理器命令行中运行命令时:Add-Migration InitialCreate 我收到以下错误:

System.Reflection.TargetInvocationException:调用的目标已抛出异常。 ---> System.TypeLoadException:无法从程序集“Microsoft.EntityFrameworkCore,Version=5.0.0.0,Culture=neutral,PublicKeyToken=adb9793829ddae60”加载类型“Microsoft.EntityFrameworkCore.Internal.SemanticVersionComparer”。 在 Microsoft.EntityFrameworkCore.Design.OperationExecutor..ctor(IOperationReportHandler reportHandler,IDictionary args) --- 内部异常堆栈跟踪结束 --- 在 System.RuntimeMethodHandle.InvokeMethod(对象目标,对象 [] 参数,签名 sig,布尔构造函数,布尔 wrapExceptions) 在 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr,Binder binder,Object[] 参数,CultureInfo 文化) 在 System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,Binder binder,Object [] args,CultureInfo 文化) 在 System.Activator.CreateInstance(类型类型、BindingFlags bindingAttr、Binder binder、Object[] args、CultureInfo 文化、Object[] activationAttributes) 在 System.Activator.CreateInstance(类型类型,对象 [] 参数) 在 Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(字符串程序集,字符串 startupAssembly,字符串 projectDir,字符串数据目录,字符串 rootNamespace,字符串语言) 在 Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor() 在 Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute() 在 Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.c__DisplayClass0_0.b__0() 在 Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args) 在 Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args) 调用的目标已抛出异常。

我听说这可能与 Microsoft.EntityFrameworkCore.Design 包有关,并且它需要更新到版本 3.0.0。只是想知道需要对哪个文件进行此更改,以及需要添加哪些必要的代码。

【问题讨论】:

  • 我认为您应该将软件包升级到 3.1.10 而不是 3.0。顺便说一句,大多数情况下,当您已经在项目/nuget 依赖项中引用 .NET 5.0 dll 时,就会发生这些错误
  • 这是版本问题。请阅读此devblogs.microsoft.com/dotnet/…。希望您阅读本文后得到解决方案
  • 我已经在命令行中运行了这个命令 - dotnet tool update --global dotnet-ef --version 5.0.0 - 并且会再试一次。
  • 现在我收到以下错误:找不到方法:'System.Collections.Generic.IEnumerable`。
  • 您的 DLL 不匹配。请发布您的目标框架以及安装了哪些 NuGet 包

标签: c# .net entity-framework-core migration


【解决方案1】:

对齐EntityFrameworkCore的Nuget包的版本号。 对于设计,您使用 5.0.0 (Ms.EntityFrameworkCore.Design),对于 Sqlite,您使用 3.1.10 (Ms.EntityFrameworkCore.Sqlite),对于 工具 3.1 .10 而对于 SqlServer,它又是 5.0.0

  1. 统一
  2. 恢复包
  3. 清理,构建解决方案/项目

【讨论】:

    【解决方案2】:

    如我在 cmets 中所述,请为所有 NuGet 包使用 v3.1.10(或可用的最新 3.x)。 这将解决您的问题

    【讨论】:

      猜你喜欢
      • 2015-07-20
      • 2019-11-15
      • 2023-04-02
      • 1970-01-01
      • 2017-02-17
      • 2023-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      相关资源
      最近更新 更多