Entity Framework 2nd Level Cache
Use GridGain.NET as an Entity Framework 6 second-level cache that stores query results in a distributed Ignite cache.
Overview
Entity Framework, as most other ORMs, can use caching on multiple levels.
First level caching is performed by
DbContexton the entity level (entities are cached within correspondingDbSet)Second level caching is on the level of
DataReaderand holds raw query data (however, there is no out-of-the-box 2nd level caching mechanism in Entity Framework 6).
GridGain.NET provides an EF6 second level caching solution that stores data in a distributed Ignite cache. This is ideal for scenarios with multiple application servers using a single SQL database via Entity Framework - cached queries are shared between all machines in the cluster.
Installation
Binary distribution: add a reference to
Apache.Ignite.EntityFramework.dllNuGet:
Install-Package Apache.Ignite.EntityFramework
Configuration
GridGain.NET provides a custom DbConfiguration implementation which enables second level caching - Apache.Ignite.EntityFramework.IgniteDbConfiguration. There is a number of ways to apply DbConfiguration to the EntityFramework DbContext. See the following MSDN document for details: msdn.microsoft.com/en-us/library/jj680699.
The simplest way to implement this is to use the [DbConfigurationType] attribute:
[DbConfigurationType(typeof(IgniteDbConfiguration))]
class MyContext : DbContext
{
public virtual DbSet<Foo> Foos { get; set; }
public virtual DbSet<Bar> Bars { get; set; }
}To customize caching behavior, create a class that inherits IgniteDbConfiguration and call one of the base constructors. The example below shows the most custom base constructor:
Caching Policy
The caching policy feature controls a selected caching mode, expiration, and which entity sets should be cached. With the default null policy, all entity sets are cached in the ReadWrite mode with no expiration. A caching policy can be configured by implementing the IDbCachingPolicy interface or inheriting the DbCachingPolicy class. The example below shows a sample implementation:
Caching Modes
ReadOnly
Read-only mode, never invalidates. Database updates are ignored in this mode. Once query results have been cached, they are kept in cache until expired (forever when no expiration is specified). This mode is suitable for data that is not expected to change (like a list of countries and other dictionary data).
ReadWrite
Read-write mode. Cached data is invalidated when underlying entity set changes. This is "normal" cache mode which always provides correct query results. Keep in mind that this mode works correctly only when all database changes are performed via DbContext with Ignite caching configured. Other database updates are not tracked.
app.config & web.config
Ignite caching can be enabled in the config files by providing an assembly-qualified type name of IgniteDbConfiguration (or your class that inherits it):
Advanced Configuration
When there is no possibility to inherit IgniteDbConfiguration (it already inherits some other class), you can call the IgniteDbConfiguration.InitializeIgniteCaching static method from the constructor, passing this as the first argument:
This page is: Copyright © 2026 MariaDB. All rights reserved.
Last updated
Was this helpful?

