一、错误信息

  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

Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation的更多相关文章

  1. The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name

    可以强迫部署EntityFramework.SqlServer.dll这个文件到输出目录 找到1个老外的帖子,戳这里(本人测试无效,大家有可能试一下..) 解决方案以下: 在EF的上下文代码CS文件( ...

  2. Unable to load type System.Data.Entity.DynamicProxiesXXXXXrequired for deserialization.

    Memcache实例的Get方法时抛出了异常“Unable to load type System.Data.Entity.DynamicProxies.AdInfoItems_19CD09C8E46 ...

  3. VS EF Error: Configuration Error extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"

    错误截图: Configuration Error :<add extension=".edmx" type="System.Data.Entity.Design. ...

  4. 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer”。请确保使用限定程序集的名称且该程序集对运行的应用程序可用。有关详细信息,请参阅 http://go.m

    Windows服务中程序发布之后会如下错误: 无法为具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序加载在应用程序配置文件中注册的实体框架提供程序类型“Syste ...

  5. system.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0) 问题

    页面控件没有做限制.提交后还可以继续点击,造成了在短时间内的多次请求.查看日志两次错误在200ms之内. 错误信息 system.Data.Entity.Infrastructure.DbUpdate ...

  6. System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidOperationException: 超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。

    2017/8/15 20:55:21 [AgentPayQuery_205506102_1BBBB]系统异常:System.Data.Entity.Core.EntityException: The ...

  7. Method not found : Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)

    找不到方法:“Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean) ...

  8. “System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常。{转}

    <connectionStrings> <add name="ConnectionStringName" providerName="System.Da ...

  9. System.Data.Entity.Internal.AppConfig"的类型初始值设定项引发异常

    在学习EF code First的小案例的时候,遇见了这个异常 <configSections> <!-- For more information on Entity Framew ...

随机推荐

  1. Microsoft Visio / Project professional 2013 官方版本(下载)

    Microsoft Visio微软开发的一款软件, 它有助于 IT 和商务专业人员轻松地可视化.分析和交流复杂信息. 它能够将难以理解的复杂文本和表格转换为一目了然的 Visio 图表. 该软件通过创 ...

  2. DDL-常见的约束

    一.常见的约束NOT NULL:非空,该字段的值必填UNIQUE:唯一,该字段的值不可重复DEFAULT:默认,该字段的值不用手动插入有默认值CHECK:检查,mysql不支持PRIMARY KEY: ...

  3. 进程通信-Queue

    进程通信-Queue Queue消息队列是python进程通信的其中一种方式.需要引入multiprocessing包中的Queue函数(这是函数,不是类). 有一个queue包,里面也有Queue, ...

  4. CUDA 版本,显卡驱动,Ubuntu版本,GCC版本之间的对应关系

  5. Flume(1)-概述与组成架构

    一. 定义 Flume是Cloudera提供的一个高可用的,高可靠的,分布式的海量日志采集.聚合和传输的系统.Flume基于流式架构,灵活简单. 二. 优点 1. 可以和任意集中式存储进程集成. 2. ...

  6. 04.flume+kafka环境搭建

    1.flume下载 安装 测试 1.1 官网下载,通过xshell从winser2012传到cent0s的/opt/flume目录中,使用rz命令 1.2 解压安装 tar -zxvf apache- ...

  7. HIve常用CLI命令

    1. 清楚屏幕:Ctrl+L 或者!Clear 最后加上分号 2. 查看数据仓库中的表:show tables; 3. 查看数据仓库中内置函数:show functions: 4. 查看表结构 :de ...

  8. Django 中跨 app 创建外键、多对多引用的方法

    问题描述 我的 Django 项目中有两个 app. 在 PersonalCenter app下的 models.py 下定义了一个 Footprint 类: 在 LoginAndRegister a ...

  9. x01.os.24: 来点代码

    <Orange'S 一个操作系统的实现>源代码 <Linux 0.11 内核完全注释>源代码 linux-0.12 源代码:  解决了 Not Owner 问题 闲来无事,在 ...

  10. 20155231 实验四 Android程序设计

    20155231 实验四 Android程序设计 实验要求 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事 ...