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:\GitVs\Lib\Sunc.Framework.Core-for-Net\Sunc.Framework.Core\Data\DbContextBase.cs:行號 32
在 Sunc.GWindInfo.DAL.DbWfContext..ctor() 位置 E:\Git\C#\Gw.Gw_Base_Info\Sunc.GWindInfo.DAL\DbWfContext.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM