.NET 6执行dotnet ef迁移命令遇TypeLoadException错误的解决方法
执行命令dotnet ef migrations add InitialCreate生成数据库迁移时,抛出如下类型加载异常:
System.TypeLoadException: 程序集'MySql.Data.EntityFrameworkCore, Version=8.0.22.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'中的类型'MySql.Data.EntityFrameworkCore.Query.Internal.MySQLSqlTranslatingExpressionVisitorFactory'的方法'Create'未实现。
在 MySql.Data.EntityFrameworkCore.Extensions.MySQLServiceCollectionExtensions.AddEntityFrameworkMySQL(IServiceCollection services)
在 MySql.Data.EntityFrameworkCore.Infrastructure.Internal.MySQLOptionsExtension.ApplyServices(IServiceCollection services)
在 Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.ApplyServices(IDbContextOptions options, ServiceCollection services)
在 Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.g__BuildServiceProvider|4_1(IDbContextOptions _, ValueTuple2 arguments) 在 Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c.<GetOrAdd>b__4_0(IDbContextOptions contextOptions, ValueTuple2 tuples)
在 System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd[TArg](TKey key, Func3 valueFactory, TArg factoryArgument)
在 Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
在 Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
在 snappfood_new_stack.Models.AppDbContext..ctor(DbContextOptions options) in /home/snappfood/RiderProjects/snappfood_new_stack/snappfood_new_stack/Models/AppDbContext.cs:line 7
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Span1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) 在 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope) 在 Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) 在 Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_3.<FindContextTypes>b__11() 在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory)
在 Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
在 Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0()
在 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)程序集'MySql.Data.EntityFrameworkCore, Version=8.0.22.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d'中的类型'MySql.Data.EntityFrameworkCore.Query.Internal.MySQLSqlTranslatingExpressionVisitorFactory'的方法'Create'未实现。
当前项目已安装的NuGet包(目标框架net6.0):
顶级包 请求版本 解析版本 Microsoft.EntityFrameworkCore 7.0.5 7.0.5 Microsoft.EntityFrameworkCore.Design 7.0.5 7.0.5 MySql.Data.EntityFrameworkCore 8.0.22 8.0.22 Swashbuckle.AspNetCore 6.2.3 6.2.3
核心原因:EF Core与MySQL驱动版本不兼容
EF Core 7.0.5属于较新的版本,但你使用的MySql.Data.EntityFrameworkCore 8.0.22仅支持到EF Core 5.x,两者API接口不匹配,导致驱动无法实现EF Core 7要求的方法,抛出类型加载异常。方案1:升级MySQL驱动至兼容EF Core 7的版本
卸载旧驱动,安装Oracle官方发布的、支持EF Core 7的最新版本(8.0.33及以上版本均兼容EF Core 7):dotnet remove package MySql.Data.EntityFrameworkCore dotnet add package MySql.Data.EntityFrameworkCore --version 8.0.33方案2:降级EF Core至MySQL驱动支持的版本
若无法升级MySQL驱动,可将EF Core相关包降级至5.x版本(比如5.0.17),确保与MySql.Data.EntityFrameworkCore 8.0.22兼容:dotnet remove package Microsoft.EntityFrameworkCore dotnet remove package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.EntityFrameworkCore --version 5.0.17 dotnet add package Microsoft.EntityFrameworkCore.Design --version 5.0.17验证与重新执行迁移
修改依赖后,先执行dotnet restore确保所有包正确加载,再重新运行迁移命令:dotnet restore dotnet ef migrations add InitialCreate
内容的提问来源于stack exchange,提问作者S k

