为什么 Entity Framework 6.1.3 抛出 "Could not load type 'System.Data.Entity.Infrastructure.TableExistenceChecker'"
Why is Entity Framework 6.1.3 throwing a "Could not load type 'System.Data.Entity.Infrastructure.TableExistenceChecker'"
全新的项目和entity framework将不会启动,因为一旦创建上下文实例就会抛出异常。
Entity framework 抛出以下异常:
Could not load type 'System.Data.Entity.Infrastructure.TableExistenceChecker' from assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
参考文献:
- EntityFramework
- EntityFramework.SqLServer
通过 nuget 包管理器:
Install-Package entityframework
非常简单的上下文和实体:
public class TextDbContext : DbContext
{
public TextDbContext()
: base("Test")
{
}
public DbSet<TestEntity> TestEntity { get; set; }
}
public class TestEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
static void Main(string[] args)
{
var test = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
using (var conn = new SqlConnection(test))
{
conn.Open();
var cmd = new SqlCommand("Select * from testtable", conn);
var result = cmd.ExecuteReader();
}
//exception thrown on this line is the same as the one in the context
var instance = SqlProviderServices.Instance;
using (var db = new TextDbContext())
{
var item = new TestEntity
{
Name = "xyz"
};
db.TestEntity.Add(item);
db.SaveChanges();
}
}
这是当前的 app.config 文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="Test" connectionString="server=localhost;database=Test;Data Source=localhost;Integrated Security=True;Pooling=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
堆栈跟踪如下:
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, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at System.Data.Entity.Utilities.MemberInfoExtensions.GetValue(MemberInfo memberInfo)
at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(Type providerType)
at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName)
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.Lazy`1.get_Value()
at System.Data.Entity.Internal.AppConfig.get_DbProviderServices()
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.RegisterDbProviderServices()
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetServiceAsServices(IDbDependencyResolver resolver, Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServices(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass6.<GetServices>b__5(IDbDependencyResolver r)
at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Linq.Enumerable.<OfTypeIterator>d__aa`1.MoveNext()
at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
at System.Data.Entity.Infrastructure.DependencyResolution.InternalConfiguration.Lock()
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.<.ctor>b__1()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.GetConfiguration()
at System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
at test2.TextDbContext..ctor() in \srv\users\carl.tierney\Documents\Visual Studio 2013\Projects\test2\test2\test2context.cs:line 13
at test2.Program.Main(String[] args) in \srv\users\carl.tierney\Documents\Visual Studio 2013\Projects\test2\test2\Program.cs:line 13
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state
at System.Threading.ThreadHelper.ThreadStart()
你有 EntityFramework.SqlServer
参考吗?这应该随 entity framework 一起自动出现。如果没有尝试将其添加为参考,或通过 Nuget。
当然,前提是您使用的是 SqlServer 提供程序。如果没有,您需要添加您的特定提供商。
显然,如果 GAC 中有对 entity framework 的引用,并且它与您通过 Nuget 引用的引用不同,则会出现此错误。在我的例子中,它是 GAC 中的 6.0.0。
解决方案:
为 visual studio 启动开发人员命令提示符,然后:
gacutil -u EntityFramework
如果您像我一样发现 Gac 中没有安装 EF,那么下一步就是在记下软件包的版本后将其卸载。我使用 NuGet,所以我去了 Tools...Library Package Manager...Package Manager Console。我先尝试了 GUI,但卸载失败,在撰写本文时,您只能安装最新版本的软件包。
- 打开您的解决方案并转到工具...库包管理器...包管理器控制台
- Select 使用 EF 的项目出现问题
- 类型卸载包EntityFramework
- 系统会提示您重新启动 Visual Studio 因此关闭并重新打开 VS 和您的解决方案
- 使用工具打开包管理器控制台...库包管理器...包管理器控制台
- 键入 Install-package EntityFramework(如果要安装旧版本,请添加 -Version x.x.x)
- 你应该可以开始了
在我的例子中,我不得不从这个文件夹中删除 EntityFramework.dll:
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\EntityFramework
安装 Visual Studio 2015 (VS 2015) 后,当 运行 使用 SQL CE 进行单元测试时,我遇到了同样的错误。我的连接工厂是 SqlCeConnectionFactory
,提供者是 System.Data.Entity.SqlServerCompact.SqlCeProviderServices
、EntityFramework.SqlServerCompact
。
我的解决方案是将 EntityFramework.SqlServerCompact.dll
的路径添加到我的 .testsettings
文件中的部署列表中。我添加的行如下所示:
<DeploymentItem filename="packages\EntityFramework.SqlServerCompact.6.1.1\lib\net45\EntityFramework.SqlServerCompact.dll" />
在我遇到此错误时,我无法在 GAC 中找到 EF。所以没有什么可以卸载的。
但是,在调查解决方案的所有项目中的所有 EF 引用后,发现其中一个项目引用了 EF 6.1.1,而所有其他项目都引用了 6.1.3。 michaelhawkins 的回答在这种情况下有所帮助,我从所有项目中删除了所有 EF,然后安装了相同的最新版本。
就把它留在这里吧,因为在所有情况下,这个异常很可能是由于 EF 版本冲突造成的,但是您具体需要查看哪里来解决冲突可能取决于各种因素。
我在单元测试项目中遇到了完全相同的问题。经过几个小时的故障排除后,我注意到 .csproj 文件仍然引用了我以前版本的 EF:
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EntityFramework.6.1.1\lib\net45\EntityFramework.dll</HintPath>
</Reference>
我只是将版本更改为 6.1.3,所有测试 运行 再次正常。
我也遇到了同样的问题
打开 Visual Studio --> 工具 --> 扩展和更新
如果有任何与 Nuget 包管理器控制台相关的更新更新它,请先检查更新。
Select 扩展和更新中的所有选项卡确保您的 Nuget 包管理器控制台版本
打开您的项目文件夹 --> Packages 然后删除关于 entityframework
的所有内容
如果有entityframework.deleteme这样的文件 --> 删掉重启Visual Studio
只需更新 package.config 文件以匹配您使用的 EF 版本
在本例中,它是“6.1.3”。
为了将来参考,在我的案例中,从 GAC 中删除 EntityFramework.SqlServer
修复了这个错误。该程序集与我的应用程序中引用的版本完全相同(EntityFramework.dll
和 EntityFramework.SqlServer.dll
都为 6.0.0.0
)。但是,发生此异常时,我在GAC中没有EntityFramework
。
我使用 NuGet 安装了对我的 SQLite Core (x86/x64) 应用程序的 EntityFramework 引用。此外,我之前曾涉足过 GAC,很可能是我自己在其中添加了程序集。
正在更改来自
的连接字符串值
"data source=.;initial catalog=[dbname];integrated security=True"
到
"Server=.;Database=[dbname];integrated security=True"
就我而言,我在两个不同的项目中工作,使用两个不同版本的 EF,当我在它们之间切换时出现问题。重新启动 VS2017 解决了问题。
另一种简单的绕过方法是使用:
EntityFramework\Add-Migration
改为
Add-Migration
全新的项目和entity framework将不会启动,因为一旦创建上下文实例就会抛出异常。
Entity framework 抛出以下异常:
Could not load type 'System.Data.Entity.Infrastructure.TableExistenceChecker' from assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
参考文献:
- EntityFramework
- EntityFramework.SqLServer
通过 nuget 包管理器:
Install-Package entityframework
非常简单的上下文和实体:
public class TextDbContext : DbContext
{
public TextDbContext()
: base("Test")
{
}
public DbSet<TestEntity> TestEntity { get; set; }
}
public class TestEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
static void Main(string[] args)
{
var test = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
using (var conn = new SqlConnection(test))
{
conn.Open();
var cmd = new SqlCommand("Select * from testtable", conn);
var result = cmd.ExecuteReader();
}
//exception thrown on this line is the same as the one in the context
var instance = SqlProviderServices.Instance;
using (var db = new TextDbContext())
{
var item = new TestEntity
{
Name = "xyz"
};
db.TestEntity.Add(item);
db.SaveChanges();
}
}
这是当前的 app.config 文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="Test" connectionString="server=localhost;database=Test;Data Source=localhost;Integrated Security=True;Pooling=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
堆栈跟踪如下:
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, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at System.Data.Entity.Utilities.MemberInfoExtensions.GetValue(MemberInfo memberInfo)
at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(Type providerType)
at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName)
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.Lazy`1.get_Value()
at System.Data.Entity.Internal.AppConfig.get_DbProviderServices()
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.RegisterDbProviderServices()
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetServiceAsServices(IDbDependencyResolver resolver, Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServices(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass6.<GetServices>b__5(IDbDependencyResolver r)
at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
at System.Linq.Enumerable.<ConcatIterator>d__71`1.MoveNext()
at System.Linq.Enumerable.<OfTypeIterator>d__aa`1.MoveNext()
at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
at System.Data.Entity.Infrastructure.DependencyResolution.InternalConfiguration.Lock()
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.<.ctor>b__1()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Lazy`1.get_Value()
at System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.GetConfiguration()
at System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
at test2.TextDbContext..ctor() in \srv\users\carl.tierney\Documents\Visual Studio 2013\Projects\test2\test2\test2context.cs:line 13
at test2.Program.Main(String[] args) in \srv\users\carl.tierney\Documents\Visual Studio 2013\Projects\test2\test2\Program.cs:line 13
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state
at System.Threading.ThreadHelper.ThreadStart()
你有 EntityFramework.SqlServer
参考吗?这应该随 entity framework 一起自动出现。如果没有尝试将其添加为参考,或通过 Nuget。
当然,前提是您使用的是 SqlServer 提供程序。如果没有,您需要添加您的特定提供商。
显然,如果 GAC 中有对 entity framework 的引用,并且它与您通过 Nuget 引用的引用不同,则会出现此错误。在我的例子中,它是 GAC 中的 6.0.0。
解决方案:
为 visual studio 启动开发人员命令提示符,然后:
gacutil -u EntityFramework
如果您像我一样发现 Gac 中没有安装 EF,那么下一步就是在记下软件包的版本后将其卸载。我使用 NuGet,所以我去了 Tools...Library Package Manager...Package Manager Console。我先尝试了 GUI,但卸载失败,在撰写本文时,您只能安装最新版本的软件包。
- 打开您的解决方案并转到工具...库包管理器...包管理器控制台
- Select 使用 EF 的项目出现问题
- 类型卸载包EntityFramework
- 系统会提示您重新启动 Visual Studio 因此关闭并重新打开 VS 和您的解决方案
- 使用工具打开包管理器控制台...库包管理器...包管理器控制台
- 键入 Install-package EntityFramework(如果要安装旧版本,请添加 -Version x.x.x)
- 你应该可以开始了
在我的例子中,我不得不从这个文件夹中删除 EntityFramework.dll:
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\EntityFramework
安装 Visual Studio 2015 (VS 2015) 后,当 运行 使用 SQL CE 进行单元测试时,我遇到了同样的错误。我的连接工厂是 SqlCeConnectionFactory
,提供者是 System.Data.Entity.SqlServerCompact.SqlCeProviderServices
、EntityFramework.SqlServerCompact
。
我的解决方案是将 EntityFramework.SqlServerCompact.dll
的路径添加到我的 .testsettings
文件中的部署列表中。我添加的行如下所示:
<DeploymentItem filename="packages\EntityFramework.SqlServerCompact.6.1.1\lib\net45\EntityFramework.SqlServerCompact.dll" />
在我遇到此错误时,我无法在 GAC 中找到 EF。所以没有什么可以卸载的。
但是,在调查解决方案的所有项目中的所有 EF 引用后,发现其中一个项目引用了 EF 6.1.1,而所有其他项目都引用了 6.1.3。 michaelhawkins 的回答在这种情况下有所帮助,我从所有项目中删除了所有 EF,然后安装了相同的最新版本。
就把它留在这里吧,因为在所有情况下,这个异常很可能是由于 EF 版本冲突造成的,但是您具体需要查看哪里来解决冲突可能取决于各种因素。
我在单元测试项目中遇到了完全相同的问题。经过几个小时的故障排除后,我注意到 .csproj 文件仍然引用了我以前版本的 EF:
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\EntityFramework.6.1.1\lib\net45\EntityFramework.dll</HintPath>
</Reference>
我只是将版本更改为 6.1.3,所有测试 运行 再次正常。
我也遇到了同样的问题
打开 Visual Studio --> 工具 --> 扩展和更新
如果有任何与 Nuget 包管理器控制台相关的更新更新它,请先检查更新。
Select 扩展和更新中的所有选项卡确保您的 Nuget 包管理器控制台版本
打开您的项目文件夹 --> Packages 然后删除关于 entityframework
的所有内容
如果有entityframework.deleteme这样的文件 --> 删掉重启Visual Studio
只需更新 package.config 文件以匹配您使用的 EF 版本 在本例中,它是“6.1.3”。
为了将来参考,在我的案例中,从 GAC 中删除 EntityFramework.SqlServer
修复了这个错误。该程序集与我的应用程序中引用的版本完全相同(EntityFramework.dll
和 EntityFramework.SqlServer.dll
都为 6.0.0.0
)。但是,发生此异常时,我在GAC中没有EntityFramework
。
我使用 NuGet 安装了对我的 SQLite Core (x86/x64) 应用程序的 EntityFramework 引用。此外,我之前曾涉足过 GAC,很可能是我自己在其中添加了程序集。
正在更改来自
的连接字符串值"data source=.;initial catalog=[dbname];integrated security=True"
到
"Server=.;Database=[dbname];integrated security=True"
就我而言,我在两个不同的项目中工作,使用两个不同版本的 EF,当我在它们之间切换时出现问题。重新启动 VS2017 解决了问题。
另一种简单的绕过方法是使用:
EntityFramework\Add-Migration
改为
Add-Migration