Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation

一、错误信息

  Entity Framework 6.0数据迁移:Add-Migration XXXX 命令发生错误

System.Reflection.TargetInvocationException: 调用的目标发生了异常。 ---> System.TypeInitializationException: “System.Data.Entity.SqlServer.SqlProviderServices”的类型初始值设定项引发异常。 ---> System.TypeLoadException: 程序集“EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中的类型“System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy”的方法“ExecuteAsync”没有实现。
在 System.Data.Entity.SqlServer.SqlProviderServices..ctor()
在 System.Data.Entity.SqlServer.SqlProviderServices..cctor()
--- 内部异常堆栈跟踪的结尾 ---
在 System.Data.Entity.SqlServer.SqlProviderServices.get_Instance()
--- 内部异常堆栈跟踪的结尾 ---
在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
在 System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
在 System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
在 System.Data.Entity.Utilities.MemberInfoExtensions.GetValue(MemberInfo memberInfo)
在 System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(Type providerType)
在 System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName)
在 System.Data.Entity.Internal.AppConfig.<.ctor>b__2(ProviderElement e)
在 System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
在 System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
在 System.Data.Entity.Internal.AppConfig.<.ctor>b__1()
在 System.Lazy`1.CreateValue()
在 System.Lazy`1.LazyInitValue()
在 System.Lazy`1.get_Value()
在 System.Data.Entity.Internal.AppConfig.get_DbProviderServices()
在 System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.RegisterDbProviderServices()
在 System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
在 System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t)
在 System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
在 System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key)
在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.<GetService>b__0(IDbDependencyResolver r)
在 System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
在 System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
在 System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
在 System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key)
在 System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver)
在 System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
在 System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
在 Sunc.Framework.Core.Data.DbContextBase..ctor(String configurationStr) 位置 E:GitVsLibSunc.Framework.Core-for-NetSunc.Framework.CoreDataDbContextBase.cs:行号 32
在 Sunc.GWindInfo.DAL.DbWfContext..ctor() 位置 E:GitC#Gw.Gw_Base_InfoSunc.GWindInfo.DALDbWfContext.cs:行号 58
--- 引发异常的上一位置中堆栈跟踪的末尾 ---
在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
在 System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func`1 resolver)
在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
在 System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
在 System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
调用的目标发生了异常。

二、问题分析

  因为项目本身框架为 .NET 4.5,而引用的DLL库为 .NET 4.0,在DLL文件中提供了 DbContext 派生的抽象类 DbContextBase,项目中通过继承 DbContextBase 进行操作,在使用Add-Migration命令便提示如上错误信息。

三、解决方案

  在我们添加引用Entity Framework时,Nuget包首先会检查我们.NET框架版本再进行包下载安装,如 EntityFramework.6.2.0包:

  

  可以看出Entity Framework 6.2.0对于.NET不同框架有着不同的引用编译版本,其原因应该是因为.NET版本中虽然存在向下兼容,但是Entity Framework针对不同的 .NET框架,内部采用的编码方式或针对新版本特性的引用则也不相同。

  此时我们应该统一框架的版本,保证Entity Framework的接口信息保持一致。

四、引用原文

  

If you check the .NET version of the two asseblies:

  • EntityFramework (v4.5)
  • EntityFramework.SqlServer (v4.0)

You will see that EntityFramework.SqlServer has v4.0 .NET dependency, but EntityFramework uses v4.5. That is the root of the issue. I use dotpeek tool for checking the assembly version (there are other options from stack overflow to check .net vestion of an assembly).

Note: and really when you decompile EntityFramework.SqlServer.dll using jetBrains reflector tool you will find that there is no ExecuteAsync method.

What we have to do to fix the issue is to use nuget.exe (from visual studio or from stand alone nuget executable: please find "latest nuget.exe"). And run it from command line:

cd "[path to your nuget.exe]"
nuget Install EntityFramework

A set of EF 6 assemblis will be downloaded. Use the EntityFramework and EntityFramework.SqlServer from .net v4.5 folder.

参考文档:Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation

原文地址:https://www.cnblogs.com/umeall/p/9582510.html