Reputation: 1121
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:
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()
Upvotes: 47
Views: 60580
Reputation: 12821
Another simple bypass is to use :
EntityFramework\Add-Migration
instead
Add-Migration
Upvotes: 1
Reputation: 1
In my case i work in two different project with two different version of EF when i switch between them the problem occurred .restarting VS2017 solved the problem .
Upvotes: 0
Reputation: 1121
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.
Solution:
Launch the developer command prompt for visual studio then:
gacutil -u EntityFramework
Upvotes: 8
Reputation: 301
I had the exact same problem in my unit test project. After a couple of hours of troubleshooting I noticed that the .csproj-file still had reference to my previous version of 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>
I simply changed the version to 6.1.3 and all tests ran fine again.
Upvotes: 10
Reputation: 908
Changing connectionstring value from
"data source=.;initial catalog=[dbname];integrated security=True"
To
"Server=.;Database=[dbname];integrated security=True"
Upvotes: 0
Reputation: 153
For future reference in my case removing EntityFramework.SqlServer
from GAC fixed this error. The assembly was exactly same version than the one referenced in my application (6.0.0.0
for both EntityFramework.dll
and EntityFramework.SqlServer.dll
). However, I didn't have EntityFramework
in GAC when this exception occurred.
I had installed the EntityFramework references to my application with SQLite Core (x86/x64) using NuGet. Also I had dabbled with GAC a bit previously and most likely added the assembly there myself.
Upvotes: 2
Reputation: 29
Just update the package.config file to match the EF version your using In this case, it is "6.1.3".
Upvotes: 1
Reputation: 1616
The same problem happened to me
Open Visual Studio --> Tools --> Extensions and Updates
Check Updates first if any update related with Nuget Package Manager Console update it.
Select All tab in Extensions and Updates be sure your Nuget Package Manager Console version
Open your project folder --> Packages then delete everything about entityframework
If there is a file like entityframework.deleteme --> delete it and restart Visual Studio
Upvotes: 3
Reputation: 3336
In my case when I got this error, I was not able to find EF in GAC. So nothing was to unistall.
However, after investigating all EF references in all projects of the solution it was found that one of the project referenced EF 6.1.1 and all others 6.1.3. Answer by michaelhawkins helped in this case, I removed all EF from all projects and then installed the same latest version back.
Just leaving it here, because in all cases this exception most probably is due to conflict of versions of EF, but where specifically you need to look to resolve the conflict may depend on various factors.
Upvotes: 6
Reputation: 31
I was getting this same error after installing Visual Studio 2015 (VS 2015) when running unit tests that use SQL CE. My connection factory is SqlCeConnectionFactory
and provider is System.Data.Entity.SqlServerCompact.SqlCeProviderServices
, EntityFramework.SqlServerCompact
.
The solution for me was to add the path to EntityFramework.SqlServerCompact.dll
to the deployment list in my .testsettings
file. The line I added looks like this:
<DeploymentItem filename="packages\EntityFramework.SqlServerCompact.6.1.1\lib\net45\EntityFramework.SqlServerCompact.dll" />
Upvotes: 1
Reputation: 69
In my case I had to remove the EntityFramework.dll from this folder:
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\EntityFramework
Upvotes: 4
Reputation: 1066
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.
Upvotes: 22
Reputation: 13888
Do you have EntityFramework.SqlServer
referenced? This should come automatically with entity framework. If not try add it as a reference, or via Nuget.
Ofcourse that is if you are usign SqlServer provider. if not, you need to add your specific provider.
Upvotes: 1