【发布时间】: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