Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsNoTracking() Method Is Missing From Context in Entity Framework

I need to load an entire table into memory using Entity Framework 4.0. I have spent the last 2 hours reading about the AsNoTracking() method that should do the trick but, I cannot figure out why the method is not available on my dataContext. Based on everything I have read, I should merely need a reference to System.Data.Entity. Then, I should be able to use the AsNoTracking() method when loading my objects. Am I missing something simple here? Is this method not available in EF 4.0? Nevertheless, below is one of the queries from my code.

// Working Query
var items = dbContext.Items.ToList()

// Does NOT Work (Compiler does not recognize AsNoTrackingMethod() )
var items = dbContext.Items.AsNoTracking().ToList()
like image 402
Grasshopper Avatar asked Feb 20 '14 20:02

Grasshopper


People also ask

What is AsNoTracking in Entity Framework Core?

The AsNoTracking() method returns a new query where the change tracker will not track any of the entities that are returned. If the entity instances are modified, this will not be detected by the change tracker, and SaveChanges() will not persist those changes to the database.

How do I turn off change tracking in Entity Framework?

In Entity Framework, change tracking is enabled by default. You can also disable change tracking by setting the AutoDetectChangesEnabled property of DbContext to false. If this property is set to true then the Entity Framework maintains the state of entities.

Which of the following method returns a new query where the entities returned will not be cached in the DbContext?

AsNoTracking(IQueryable) Returns a new query where the entities returned will not be cached in the DbContext or ObjectContext. This method works by calling the AsNoTracking method of the underlying query object.


2 Answers

If using EF from .NET Core use directive:

using Microsoft.EntityFrameworkCore;
like image 124
Mitja Gomboc Avatar answered Sep 21 '22 14:09

Mitja Gomboc


AsNoTracking() is an extension method in the DbExtensions (EF5)/QueryableExtensions (EF6) class, which is part of the System.Data.Entity namespace. It is not missing from Entity Framework 4.1+. You simply need to remember to add a using directive for that namespace.

using System.Data.Entity;
like image 35
user1417835 Avatar answered Sep 21 '22 14:09

user1417835