• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
  • Products
  • Solutions
  • Customers
  • Resources
  • Company
  • Pricing
  • Download
Download
  • .NET Third Party Integrations
  • Entity Framework EF 6
  • EF Second Level Cache
Show / Hide Table of Contents
  • Programmer's Guide
  • Setting Up Development Environment
    • .NET
      • Client API Prerequisites
      • Server-side API Prerequisites
    • Java
      • Client API Prerequisites
    • Python
      • Client API Prerequisites
    • Node.js
      • Client API Prerequisites
  • Client Side API Programming
    • Error Handling
    • Troubleshooting
    • Cache Keys and Data
    • How to Connect to Cache
    • Basic Operations - An Overview
      • Add Data
      • Update/Insert Data
      • Retrieve Data
      • Remove Data
    • Groups
      • Overview
      • Add/Update Data with Groups
      • Retrieve Data with Groups
      • Remove Data with Group
      • Search Group Data Using SQL
      • Delete Group Data Using SQL
    • Tags
      • Overview
      • Add/Update Data with Tags
      • Retrieve Data with Tags
      • Remove Data with Tags
      • Search Tag Data Using SQL
      • Delete Tag Data Using SQL
    • Named Tags
      • Overview
      • Add/Update Data with Named Tags
      • Remove Data with Named Tags
      • Search Data with Named Tags Using SQL
      • Delete Data with Named Tags Using SQL
    • Expirations
      • Overview
      • Absolute Expiration
      • Sliding Expiration
    • Data Dependency
      • Key Dependency
      • Multi-Cache Dependency
    • Dependency on Database
      • SQL Dependency
      • Oracle Dependency
      • OleDB Dependency
      • CLR Procedures in SQL Server
    • Dependency on External Source
      • File Dependency
      • Custom Dependency
      • Aggregate Dependency
    • Locks
      • Types of Locking
      • Pessimistic Locking
      • Optimistic Locking
    • SQL Query
      • Overview
      • Define Indexes Programmatically
      • Query with ExecuteReader and ExecuteScalar
      • Delete Data with ExecuteNonQuery
      • SQL Reference
    • LINQ Query
      • Overview
      • LINQ Query for Objects
      • LINQ Reference
    • Data Structures
      • Overview
      • List
      • Queue
      • Set
      • Dictionary
      • Counter
      • Invalidation Attributes
      • Searchable Attributes
      • Query on Data Structures
      • Remove from Data Structure
    • Events
      • Overview
      • Cache Level Events
      • Item Level Events
      • Management Level Events
    • Pub/Sub Messaging
      • Overview
      • Pub/Sub Topics
      • Publish Messages to a Topic
      • Subscribe to a Topic
      • Monitoring Pub/Sub Topics
    • Continuous Query
      • Overview
      • Use Continuous Query
    • Stream Processing
      • Add/Update Stream Data
      • Retrieve Stream Data
    • JSON
      • Overview
      • Use JSON Objects
      • Query JSON Data
    • Security API
      • Login with Credentials
    • Management API
    • Error Logging
    • Location Affinity
  • Server-side API Programming
    • Loader and Refresher
      • Overview
      • Implement Loader and Refresher
      • Components of Loader/Refresher
    • Data Source Providers
      • Read-through
        • Implement Read-through
        • Use Read-through
      • Write-through
        • Implement Write-through
        • Use Write-through
        • Use Write-behind
    • Custom Dependency
      • Implement Extensible Dependency
      • Implement Bulk Extensible Dependency
      • Implement Notify Extensible Dependency
    • Bridge Conflict Resolver
    • Entry Processor
      • Overview
      • Implement Entry Processor
    • MapReduce
      • Overview
      • Implement MapReduce
      • Use MapReduce
    • MapReduce Aggregator
      • Overview
      • Implement and Use Aggregator
    • Compact Serialization
  • Client Side Features
    • ASP.NET Core Caching
      • Session Storage
        • Session Provider
        • IDistributedCache
        • Sessions Usage
        • Multi-site Session Provider
        • Session Sharing with ASP.NET
      • SignalR
        • SignalR Core Integration for NCache
      • Response Caching
        • Configure and Use
        • Configure with IDistributedCache
      • Data Caching
        • NCache API
        • IDistributedCache API
      • Data Protection Provider
        • Configure
    • Java Web App Caching
      • Web Sessions
        • Overview
        • Configure App
          • Add Maven Dependencies
          • Deploy Application
        • Multi-site Sessions
    • Node.js App Caching
      • Web Sessions
    • ASP.NET Caching Benefits and Overview
      • ASP.NET Session State Provider Properties
      • Multi-region ASP.NET Session State Provider Configuration
      • Session Sharing between ASP.NET and ASP.NET Core
      • ASP.NET SignalR Backplane
        • NCache Extension for SignalR
      • ASP.NET View State Caching
        • View State Content Optimization Configuration
        • Group View State with Sessions
        • Limit View State Caching
        • Page Level Grouping
      • ASP.NET Output Cache
        • Output Caching Provider Overview
        • Output Cache with Custom Hooks
  • .NET Third Party Integrations
    • Entity Framework (EF) Core
      • Installation
      • Configure
      • EF Core Extension Methods
        • Extension Methods
        • Cache Handle
        • Caching Options
        • Query Deferred API
      • Logging in EF Core
    • Entity Framework EF 6
      • EF Second Level Cache
      • EF Caching Resync Provider
      • EF Caching Configuration File
    • NHibernate
      • Second Level Cache
      • Query Caching
      • Synchronize Database with Cache
    • Debug NCache Providers in Visual Studio
  • Java Third Party Integrations
    • Hibernate
      • Second Level Cache
      • Configure Cacheable Objects and Regions
      • Configure Application
      • Query Caching
    • Spring
      • Overview
      • Use NCache with Spring
        • Configure Generic Provider
        • Configure JCache Spring Caching Provider
        • Configure Caching Declaration
        • Configure Spring Sessions
    • JCache API
      • CRUD Operations
      • Expiration
      • Events
  • Third-Party Wrappers
    • AppFabric to NCache Migration
      • AppFabric API vs. NCache API
      • Configuration Differences Between AppFabric and NCache
      • Migrating from an AppFabric Application to NCache
    • Redis to NCache Migration
      • Redis to NCache Migration
    • Memcached Wrapper
      • Memcached Gateway Approach
      • Memcached Client Plugin for .NET

Second-level Entity Framework Cache [Deprecated]

NCache Entity Framework Caching Provider can be integrated into the application without any code modification. By simply updating the configuration files and specifying NCache Entity Framework Caching Provider as a second-level Cache provider, as detailed below.

Prerequisites for Second-level Entity Framework Cache

  • .NET
  • Install the following NuGet package in your application:
    • EntityFramework6.NCache
  • Sample Entity Framework application which can be found on GitHub.
  • Microsoft Visual Studio 2022 for Entity Framework 6.1.3 and 6.5.1.
  • Database Tools (MS SQL Server, Oracle).

Steps required to enable NCache as Second-level Cache Provider in Entity Framework application are as follow.

Reference Assembly

To utilize the Provider, first install the NuGet package for Entity Framework in your application. Then include the namespace Alachisoft.Integrations.EntityFramework.CachingProvider to get started.

There are two methods through which you can integrate your Entity Framework application with the NCache caching provider. One is Modifying Application or Web Configurations and the other is Programming Method.

Modifying Application or Web Configurations

App.config or Web.config which is generated on adding ADO.NET Entity Data Model in Entity Framework application requires the following changes to be done.

Specify Provider Invariant Name

Provider Invariant Name is specified either in the connectionString of application or web configuration file, or programmatically when initializing EntityConnection.

Entity Framework 3.5-4.0

<connectionStrings>
     <add name="NorthwindEntities" connectionString="metadata=res://*/NorthwindCustomerModel.csdl |res://*/NorthwindCustomerModel.ssdl |res://*/NorthwindCustomerModel.msl; provider=Alachisoft.Integrations.EntityFramework.CachingProvider; provider connection string=&quot;
     wrappedProvider=System.Data.SqlClient; Data Source=localhost;Initial Catalog=Northwind;user id= userid; password=password; MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>
</connectionStrings>

Add the provider factory for initialization of NCache Entity Framework Provider. The invariant attribute should be the same as the provider name in connectionString.

<DbProviderFactories>
    <add name="EF Caching Data Provider" invariant="Alachisoft.Integrations.EntityFramework.CachingProvider" description="Caching Provider Wrapper" type="Alachisoft.NCache.Integrations.EntityFramework.EFCachingProviderFactory, Alachisoft.Integrations.EntityFramework.CachingProvider, Version=x.x.x.x,Culture=neutral, PublicKeyToken=cff5926ed6a53769"/>
</DbProviderFactories>
Note

Replace Version=x.x.x.x with the actual NCache version that you have installed.

Programming (Alternative Method)

Another method for specifying connection is specifying it while creating ObjectContext or EntityConnection. The example below shows the creation of a connectionString programmatically and the initialization of EntityConnection.

    SqlConnectionStringBuilder sqlConnBuilder = new SqlConnectionStringBuilder();
    sqlConnBuilder.DataSource = "localhost";
    sqlConnBuilder.InitialCatalog = "EFTestDB";
    sqlConnBuilder.IntegratedSecurity = true;
    string conString = sqlConnBuilder.ToString();
    EntityConnectionStringBuilder efConnBuilder = new EntityConnectionStringBuilder();
    efConnBuilder.Provider = "EFCachingProvider";
    efConnBuilder.ProviderConnectionString = @"wrappedProvider=System.Data.SqlClient;" + conString;
    efConnBuilder.Metadata = "res:// /NorthwindCustomerModel.csdl|res:" + "// /NorthwindCustomerModel.ssdl|res:" +"// /NorthwindCustomerModel.msl;";
    EntityConnection connection = new EntityConnection(efConnBuilder.ToString());

Entity Framework 6.1 and 6.2

Overwrite the invariantName and type configurations to specify NCache Entity Framework Provider as shown below. Add an interceptor in <provider> section in the application (or web) configuration file:

<entityFramework>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <interceptors>
        <interceptor type="Alachisoft.NCache.Integrations.EntityFramework.Caching.EFCommandInterceptor, Alachisoft.Integrations.EntityFramework.CachingProvider"/>
    </interceptors>
</entityFramework>

Programming (Alternative Method)

The provider variant can also be specified while initializing the configuration. Given below is an example showing how provider invariant can be specified.

this.AddInterceptor(new EFCommandInterceptor());

You can now cache results using extension provided for IQueryable by following the steps below.

  • Specify the following namespace.
using Alachisoft.NCache.Integrations.EntityFramework.Caching;
  • Call the Cache method overloads now from IQueryable instance.
IQueryable<Course> courses;

// courses = <some query according to your requirement>

var courses = courses.Cache();

Add Application ID to appSettings

The following information should be added in <appSettings> of App.config or Web.config.

Note

These settings are independent of Entity Framework version.

<appSettings>
    <add key="app-id" value="EFCachingDemo"/>
    <add key="logging-level" value="Debug"/>
</appSettings>
  • app-id: This will be the unique identifier for the current application. NCache Entity Framework Caching Provider reads the required configuration from efcaching.ncconf based on the ID given in App.config or Web.config file of your application.

  • logging-level: Determines the logging level for the application. The Provider will log exceptions and errors only when the level is set to Error. When the level is Debug, both exceptions/errors and other detailed information will be logged. Nothing will be logged if the level is set to Off (Default). The path at which log files will be generated is %NCHOME%\log-files\EfCachingLogs (Windows) or /opt/ncache/log-files/EfCachingLogs (Linux) and the path at which provider-related logs will be generated is %NCHOME%\log-files\EfCachingLogs\EfCachingProviderLogs (Windows).

Note
  • App-id of efcaching.ncconf should be the same as the application (App.config) to avoid an error.

Alternative method through Programming

Apart from specifying the connectionString in App.config or Web.config, the connection can also be specified while creating ObjectContext or EntityConnection. While creating a connectionString programmatically, the wrapper information must be included in a connectionString. Here is how the connectionString can be created and ObjectContext or EntityConnection can be initialized.

  • .NET
  • Legacy API
var sqlConnBuilder = new SqlConnectionStringBuilder();

sqlConnBuilder.DataSource = "localhost";
sqlConnBuilder.InitialCatalog = "EFTestDB";
sqlConnBuilder.IntegratedSecurity = true;

string conString = sqlConnBuilder.ToString();

var efConnBuilder = new EntityConnectionStringBuilder();

efConnBuilder.Provider = "EFCachingProvider";
efConnBuilder.ProviderConnectionString = @"wrappedProvider=System.Data.SqlClient;" + conString;
efConnBuilder.Metadata = "res:// /NorthwindCustomerModel.csdl|res:" + "// /NorthwindCustomerModel.ssdl|res:" +"// /NorthwindCustomerModel.msl;";

var connection = new EntityConnection(efConnBuilder.ToString());
SqlConnectionStringBuilder sqlConnBuilder = new SqlConnectionStringBuilder();

sqlConnBuilder.DataSource = "localhost";
sqlConnBuilder.InitialCatalog = "EFTestDB";
sqlConnBuilder.IntegratedSecurity = true;

string conString = sqlConnBuilder.ToString();

EntityConnectionStringBuilder efConnBuilder = new EntityConnectionStringBuilder();

efConnBuilder.Provider = "EFCachingProvider";
efConnBuilder.ProviderConnectionString = @"wrappedProvider=System.Data.SqlClient;" + conString;
efConnBuilder.Metadata = "res:// /NorthwindCustomerModel.csdl|res:" + "// /NorthwindCustomerModel.ssdl|res:" +"// /NorthwindCustomerModel.msl;";

EntityConnection connection = new EntityConnection(efConnBuilder.ToString());
Note

The application will need to be rebuilt to effectively apply the above changes.

See Also

Entity Framework Resync Provider
Entity Framework Caching Config File
NHibernate
Entity Framework Core Caching

In This Article
  • Prerequisites for Second-level Entity Framework Cache
  • Reference Assembly
  • Modifying Application or Web Configurations
    • Specify Provider Invariant Name
    • Add Application ID to appSettings
  • Alternative method through Programming
  • See Also

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Community
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - 2025. All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top