由于在创建上下文实例后抛出异常,全新的项目和实体框架将无法启动 .
实体框架抛出以下异常:
无法从程序集'EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'加载类型'System.Data.Entity.Infrastructure.TableExistenceChecker' .
参考文献:
-
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()
11 回答
如果您发现我没有在Gac中安装EF,那么下一步就是在您注意到包的版本之后将其卸载 . 我使用NuGet,所以我去了Tools ... Library Package Manager ... Package Manager Console . 我首先尝试了GUI,但卸载失败,在撰写本文时,您只能安装最新版本的软件包 .
打开解决方案并转到工具...库包管理器...包管理器控制台
选择使用EF的项目并遇到问题
键入Uninstall-package EntityFramework
系统应提示您重新启动Visual Studio,然后重新打开VS和您的解决方案
使用工具打开包管理器控制台...库包管理器...包管理器控制台
类型Install-package EntityFramework(如果您要安装旧版本,请添加-Version x.x.x)
你应该好好去
我的单元测试项目中遇到了完全相同的问题 . 经过几个小时的故障排除后,我注意到.csproj文件仍然引用了我之前的EF版本:
我只是将版本更改为6.1.3,所有测试再次运行正常 .
显然,如果GAC中存在对实体框架的引用,并且它与您通过Nuget引用的实体框架不同,则会出现此错误 . 就我而言,它在GAC中是6.0.0 .
解:
启动Visual Studio的开发人员命令提示符:
就我而言,当我收到此错误时,我无法在GAC中找到EF . 所以没有什么是unistall .
但是,在调查解决方案的所有项目中的所有EF引用之后,发现其中一个项目引用了EF 6.1.1,所有其他项目引用了6.1.3 . 通过michaelhawkins的回答在这种情况下,我从所有项目中删除了所有EF,然后安装了相同的最新版本 .
只是把它留在这里,因为在所有情况下,这种异常很可能是由于EF版本的冲突,但具体而言,你需要寻求解决冲突可能取决于各种因素 .
在我的情况下,我不得不从这个文件夹中删除EntityFramework.dll:
同样的问题发生在我身上
打开Visual Studio - >工具 - >扩展和更新
如果与Nuget Package Manager控制台相关的任何更新更新,则首先检查更新 .
在“扩展和更新”中选择“全部”选项卡,确保您的Nuget包管理器控制台版本
打开项目文件夹 - >包然后删除有关entityframework的所有内容
如果有像entityframework.deleteme这样的文件 - >删除它并重新启动Visual Studio
你引用了
EntityFramework.SqlServer
吗?这应该是实体框架自动生成的 . 如果没有尝试添加它作为参考,或通过Nuget .当然,如果你是使用SqlServer提供程序 . 如果没有,您需要添加您的特定提供商 .
只需更新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,并且很可能自己也在那里添加了程序集 .
在运行使用SQL CE的单元测试时,在安装Visual Studio 2015(VS 2015)后,我遇到了同样的错误 . 我的连接工厂是
SqlCeConnectionFactory
,提供商是System.Data.Entity.SqlServerCompact.SqlCeProviderServices
,EntityFramework.SqlServerCompact
.我的解决方案是将
EntityFramework.SqlServerCompact.dll
的路径添加到我的部署列表中.testsettings
文件 . 我添加的行看起来像这样:更改的连接字符串值
至