Try Playground
Show / Hide Table of Contents

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
  • 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 Professional
  • 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