为什么实体框架6.1.3扔"未能加载类型“System.Data.Entity.Infrastructure.TableExistenceChecker'"实体、框架、加载

2023-09-04 22:34:59 作者:天涯浪人

全新的项目和实体框架将无法启动,由于异常被尽快上下文实例被创建抛出。

实体框架抛出以下异常:

  

未能从程序集加载类型System.Data.Entity.Infrastructure.TableExistenceChecker'的EntityFramework,版本= 6.0.0.0,文化=中性公钥= b77a5c561934e089'。

参考文献:

的EntityFramework EntityFramework.SqLServer

经由的NuGet包管理器:

 安装,包装的EntityFramework
 

很简单的背景和实体:

 公共类TextDbContext:的DbContext
{
    公共TextDbContext()
        :基地(测试)
    {
    }

    公共DbSet< TestEntity> TestEntity {获得;组; }
}

公共类TestEntity
{
    公众诠释编号{获得;组; }
    公共字符串名称{;组; }
}

静态无效的主要(字串[] args)
{
    VAR测试= ConfigurationManager.ConnectionStrings [测试]的ConnectionString。

    使用(VAR康恩=新的SqlConnection(测试))
    {
        conn.Open();
        VAR CMD =新的SqlCommand(选择TestTable的*,康涅狄格州);
        VAR的结果= cmd.ExecuteReader();
    }
    //抛出的异常在这条线是相同的所述一个在上下文
    VAR实例= SqlProviderServices.Instance;

    使用(VAR DB =新TextDbContext())
    {
         VAR项目=新TestEntity
         {
             NAME =XYZ
         };
         db.TestEntity.Add(项目);
         db.SaveChanges();
    }
}
 

下面是当前app.config文件:

< XML版本=1.0编码=UTF-8&GT?; <结构>     < configSections>         <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本= 6.0.0.0,文化=中性公钥= b77a5c561934e089requirePermission =FALSE/>     < / configSections>     <的ConnectionStrings>         <添加名称=测试的connectionString =服务器=本地主机;数据库=测试;数据源=本地主机;集成安全性= TRUE;池=真的providerName =System.Data.SqlClient的/>     < /的ConnectionStrings>     <启动>        < supportedRuntime版本=4.0版的SKU =NETFramework,版本= V4.5/>     < /启动>     <的EntityFramework>         < defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.SqlConnectionFactory,的EntityFramework/>          <供应商>             <供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>         < /供应商>     < /的EntityFramework> < /结构> 用cmd可以编译java源程序,但是运行时却提示错误 找不到或无法加载主类,这是什原因啊

堆栈跟踪如下:

 在System.RuntimeMethodHandle.InvokeMethod(对象的目标,对象[]参数,签名的签名,布尔构造函数)
   在System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(obj对象,对象[]参数,对象[]参数)
   在System.Reflection.RuntimeMethodInfo.Invoke(obj对象,的BindingFlags invokeAttr,粘合剂粘合,对象]参数,CultureInfo的文化)
   在System.Reflection.RuntimePropertyInfo.GetValue(obj对象,的BindingFlags invokeAttr,粘合剂粘合,对象[]指标,CultureInfo的文化)
   在System.Reflection.RuntimePropertyInfo.GetValue(obj对象,对象[]索引)
   在System.Data.Entity.Utilities.MemberInfoExtensions.GetValue(的MemberInfo的MemberInfo)
   在System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(类型providerType)
   在System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(字符串providerTypeName,字符串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集合)
   在System.Linq.Enumerable.ToList [TSource](IEnumerable`1源)
   在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类型,字符串名称)
   在System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.<GetService>b__0(Tuple`2 T)
   在System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKEY的关键,Func`2 valueFactory)
   在System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(类型类型,对象键)
   在System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetServiceAsServices(IDbDependencyResolver解析器,类型类型,对象键)
   在System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServices(类型类型,对象键)
   在System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass6.<GetServices>b__5(IDbDependencyResolver R)
   在System.Linq.Enumerable&LT; SelectManyIterator&GT; d__14`2.MoveNext()
   在System.Linq.Enumerable&LT; ConcatIterator&GT; d__71`1.MoveNext()
   在System.Linq.Enumerable&LT; OfTypeIterator&GT; d__aa`1.MoveNext()
   在System.Data.Entity.Utilities.IEnumerableExtensions.Each [T](IEnumerable`1 TS,Action`1动作)
   在System.Data.Entity.Infrastructure.DependencyResolution.InternalConfiguration.Lock()
   在System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager&LT; .ctor&GT; b__1()
   在System.Lazy`1.CreateValue()
   在System.Lazy`1.LazyInitValue()
   在System.Lazy`1.get_Value()
   在System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.GetConfiguration()
   在System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection了InternalConnection,DbCompiledModel模型)
   在System.Data.Entity.DbContext..ctor(字符串nameOrConnectionString)
   在test2.TextDbContext..ctor()的\\ SRV \用户\ carl.tierney \文档\的Visual Studio 2013 \项目\ TEST2 \ TEST2 \ test2context.cs:13号线
   在test2.Program.Main(字串[] args)的\\ SRV \用户\ carl.tierney \文档\ Visual Studio的2013 \项目\ TEST2 \ TEST2 \的Program.cs:行13
   在System.AppDomain._nExecuteAssembly(RuntimeAssembly组装,字串[] args)
   在System.AppDomain.ExecuteAssembly(字符串assemblyFile,证据assemblySecurity,字串[] args)
   在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
   在System.Threading.ExecutionContext.RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)
   在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象的状态,布尔preserveSyncCtx)
   在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态
   在System.Threading.ThreadHelper.ThreadStart()
 

解决方案

如果你觉得像我这样做,EF未安装在GAC然后下一步之后,你注意你的包的版本卸载它。我使用的NuGet让我去的工具...库包管理器...软件包管理器控制台。我尝试了GUI第一,但在卸载失败,因为写这篇文章,你可以只安装软件包的最新版本。

打开您的解决方案,并转到工具...库包管理器...软件包管理器控制台 选择使用EF和是该项目遇到的问题 类型卸载包的EntityFramework 您应该会提示重新启动Visual Studio这样关闭并重新打开VS和您的解决方案 开启程序包管理器控制台与工具...库包管理器...软件包管理器控制台 键入安装包的EntityFramework(添加-Version XXX,如果你安装的是旧版本) 您应该好走

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:

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

References:

EntityFramework EntityFramework.SqLServer

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();
    }
}

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>

Stack trace is as follows:

   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()

解决方案

If you find as I did that EF is not installed in the Gac then the next step is to uninstall it AFTER you note the version of your package. I use NuGet so I went to Tools...Library Package Manager...Package Manager Console. I tried the GUI first but uninstalling failed and as of this writing you can only install the latest version of the package.

Open your solution and go to Tools...Library Package Manager...Package Manager Console Select the project that uses EF and is having the problem Type Uninstall-package EntityFramework You should be prompted to restart Visual Studio so close and reopen VS and your solution Open Package Manager Console with Tools...Library Package Manager...Package Manager Console Type Install-package EntityFramework (add -Version x.x.x if you're installing an older version) You should be good to go