Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop entity framework caching

We're having a problem testing Entity Framework 4.

We've deployed a WCF service that implements an EF data context. All works fine untill we modify the data using SQL server studio.

Is there a way to stop EF caching our results or is there any way to turn eager loading on?

Cheers,

James

like image 577
Zack Avatar asked Dec 03 '10 14:12

Zack


People also ask

Does Entity Framework cache data?

Entity Framework has the following forms of caching built-in: Object caching – the ObjectStateManager built into an ObjectContext instance keeps track in memory of the objects that have been retrieved using that instance. This is also known as first-level cache.

Does EF core cache data by default?

The Cache: The memory cache is used by default.

Does Entity Framework cache queries?

The first time a query is invoked, data is retrieved from the database and stored in memory before being returned. The compiled queries could be cached for the life of an app pool instance and provided to the Entity Framework for low latency performance of data access.

What difference does AsNoTracking () make?

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

On the property sheet for your model, you can set the Lazy Loading Enabled property.

alt text

Through code, you can control lazy loading with the ObjectContextOptions.LazyLoadingEnabled property:

context.ContextOptions.LazyLoadingEnabled = false;
like image 115
Jeff Ogata Avatar answered Oct 15 '22 15:10

Jeff Ogata


In EF4 I had to use this instead:

_context.Configuration.LazyLoadingEnabled = false;
like image 21
Lodlaiden Avatar answered Oct 15 '22 16:10

Lodlaiden