无法从程序集“Microsoft.EntityFrameworkCore”加载类型“Microsoft.EntityFrameworkCore.Internal.SemanticVersionComparer”,
c#
我有一个使用 EF Core 的 ASP.NET Core 3.1 Web API 应用程序。这是我在类的ConfigureServices方法中的配置Startup:
services.AddDbContext<ApplicationContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("AppConn")));
上述配置已测试并适用于 SQL Server 数据库。
然后我在成功安装它的包后切换到使用 Sqlite。
services.AddDbContext<ApplicationContext>(options =>
options.UseSqlite("Data Source=sqlitedemo.db"));
但是当我尝试添加 EF 迁移时
add-migration initial -context ApplicationContext
我收到此错误:
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(IOOperationReportHandler reportHandler, IDictionary args)
--- 内部异常堆栈跟踪结束 ---
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean 构造函数, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfoculture)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfoculture)
at System .Activator.CreateInstance(类型类型,BindingFlags bindingAttr,Binder binder,Object[] args,CultureInfo 文化,Object[] activationAttributes)
在 System.Activator.CreateInstance(Type type, Object[] args)
在 Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language)
在 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)调用的目标已抛出异常。
回答
请将您的实体框架核心 nuget 包更新到 3.1.10(或最新的 5.0.0)。它会解决你的问题。
回答
我安装了 EF Core 5 包,但没有安装,Microsoft.EntityFrameworkCore.Design一个包隐式引用旧版本 ( Microsoft.EntityFrameworkCore.Design 3.0.0)。
安装显式依赖Microsoft.EntityFrameworkCore.Design 5.x.x为我解决了这个问题。