sql-server - 是什么导致 EF6 的 Migrate.exe 出现 TypeLoadException?

标签 sql-server entity-framework entity-framework-migrations

我正在尝试从命令行运行 EF 的 migrate.exe,作为构建过程的一部分。这是我的实际命令:

migrate.exe Hydrogen.Data.dll /startupConfigurationFile:..\..\..\Hydrogen.Web\Web.config 
/verbose

并且它不断抛出此异常:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'System.Data.Entity.SqlServer.SqlProviderServices' threw an exception. ---> System.TypeLoadException: Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' from assembly 'EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' does not have an implementation.
   at System.Data.Entity.SqlServer.SqlProviderServices..ctor()
   at System.Data.Entity.SqlServer.SqlProviderServices..cctor()
   --- End of inner exception stack trace ---
   at System.Data.Entity.SqlServer.SqlProviderServices.get_Instance()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at System.Data.Entity.Config.ProviderServicesFactory.GetInstance(Type providerType)
   at System.Data.Entity.Internal.AppConfig.<.ctor>b__2(ProviderElement e)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Entity.Internal.AppConfig.<.ctor>b__1()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Data.Entity.Internal.AppConfig.get_DbProviderServices()
   at System.Data.Entity.Config.AppConfigDependencyResolver.RegisterDbProviderServices()
   at System.Data.Entity.Config.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at System.Data.Entity.Config.AppConfigDependencyResolver.GetService(Type type, Object key)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Config.CompositeResolver`2.GetService(Type type, Object key)
   at System.Data.Entity.Config.IDbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
   at System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
   at Hydrogen.Data.HydrogenData..ctor() in c:\code\Hydrogen\Hydrogen.Data\HydrogenData.cs:line 18
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at System.Data.Entity.Infrastructure.DbContextInfo.<CreateActivator>b__2()
   at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo)
   at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbConnectionInfo connectionInfo)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Console.Program.Run()
   at System.Data.Entity.Migrations.Console.Program.Main(String[] args)
ERROR: Exception has been thrown by the target of an invocation.

当我从 Visual Studio 中运行 Update-Database 时,一切正常,因此我知道迁移本身正在运行。

这是在 SQL Server 数据库上。

最佳答案

我今天刚遇到这个。当您运行 migrate.exe 时使用了错误的 DLL 集时,可能会发生这种情况。

就我而言,我在 migrate.exe 中使用了 .Net 4.5 版本,但使用 .Net 4.0 版本编译了我的项目。

更改 dll 后问题就消失了。

关于sql-server - 是什么导致 EF6 的 Migrate.exe 出现 TypeLoadException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17572705/

相关文章:

c# - Entity Framework : am I supposed to modify migration classes?

c# - EF 插入数据库生成的 key ,忽略 DatabaseGeneratedOption.None

sql-server - 用户“IIS APPPOOL\”登录失败

mysql - SQL 存储过程 VS 外部语言过程

c# - 使用 EntityFramework 获取有序范围的最有效方法是什么?

c# - 无法转换异常类型的对象

SQL - SPACE(2) + '|' + SPACE(2) 和 ‘ | 之间的区别'?

sql - T-SQL 将一组行与其他行组进行比较

c# - LINQ to Entities 选择带有表达式参数的列

sql - 更新 DB 时出错 : SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified