为什么实体框架 6.1.3 会抛出“无法加载类型‘System.Data.Entity.Infrastructure.TableExistenceChecker’"? [英] Why is Entity Framework 6.1.3 throwing a "Could not load type 'System.Data.Entity.Infrastructure.TableExistenceChecker'"

查看:25
本文介绍了为什么实体框架 6.1.3 会抛出“无法加载类型‘System.Data.Entity.Infrastructure.TableExistenceChecker’"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全新的项目和实体框架将不会启动,因为一旦创建上下文实例就会引发异常.

Brand new project and entity framework will not start due to the exception being thrown as soon as the context instance is created.

实体框架抛出以下异常:

Entity framework throws the following exception:

无法从程序集EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"中加载类型System.Data.Entity.Infrastructure.TableExistenceChecker".

Could not load type 'System.Data.Entity.Infrastructure.TableExistenceChecker' from assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

参考资料:

  • 实体框架
  • EntityFramework.SqlServer

通过 nuget 包管理器:

Via the nuget package manager:

Install-Package entityframework

非常简单的上下文和实体:

Very simple context and entity:

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 文件:

Here is the current app.config file:

<?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 \srvuserscarl.tierneyDocumentsVisual Studio 2013Projects	est2	est2	est2context.cs:line 13
   at test2.Program.Main(String[] args) in \srvuserscarl.tierneyDocumentsVisual Studio 2013Projects	est2	est2Program.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()

推荐答案

显然,如果 GAC 中存在对实体框架的引用,并且它与您通过 Nuget 引用的不同,则会出现此错误.就我而言,它在 GAC 中是 6.0.0.

Apparently if there is a reference to entity framework in the GAC and it is not the same as the one you have referenced via Nuget you get this error. In my case it was 6.0.0 in the GAC.

解决办法:

然后启动 Visual Studio 的开发人员命令提示符:

Launch the developer command prompt for visual studio then:

gacutil -u EntityFramework

这篇关于为什么实体框架 6.1.3 会抛出“无法加载类型‘System.Data.Entity.Infrastructure.TableExistenceChecker’"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆