Alachisoft NCache 4.1 - Online Documentation

Enabling NCache in Entity Framework application

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
Following are the prerequisites to enable NCache in Entity Framework application:
 
1. You should have an Entity Framework application. (you can also use sample application for EFCaching from NCache sample applications from the following path:"Installeddirectory:/ProgramFiles/NCache/samples/clr20/EntityDataModelIntegrationDemo"
In every Entity Framework application, ADO.NET Entity Data Model is already added which automatically generates two files:
  • SSDL
  • Application (or web) configuration file
2. Microsoft Visual Studio 2008 with SP1.
3. Database Tool (e.g. MS SQL SERVER 2005, ORACLE)
 
Steps required to enable NCache in Entity Framework application:
 
Step 1: Add Reference:
 
Add "Alachisoft.Integrations.EntityFramework.CachingProvider" reference to your Entity Framework application. This .dll file is placed on the following path: "Installeddirectory:/ProgramFiles/NCache/integration/MSEntityFramework".
 
After adding the reference, changes are required in 3 different files:
 
Step 2: SSDL Configurations:
 
In SSDL file which is generated on adding ADO.NET Entity Data Model in Entity Framework application, required following changes:
 
[Below is the Sample change for Sql 2005 database]
 
In SSDL, the provider name is specified in the Provider attribute of the <Schema/> element as shown below:
 
[Existing Code]
 
<Schema Namespace = "NorthwindModel.Store" Alias = "Self" Provider = "System.Data.SqlClient" ProviderManifestToken = "2005" xmlns:store = "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns = "http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
 
In order to inject our NCache Entity frame Provider, we need to override the above highlighted attributes to plugin our provider. In SSDL, we put the name of the new provider in the Provider attribute and concatenate the previous provider with our provider manifest token in the ProviderManifestToken field, as shown below:
 
[Changed Code]
 
<Schema Namespace = "NorthwindModel.Store" Alias = "Self" Provider = "Alachisoft.Integrations.EntityFramework.CachingProvider" ProviderManifestToken = "System.Data.SqlClient;2005" xmlns:store = "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns = "http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
 
 
Step 3: Application (or web) Configurations:
 
Application (or web) configuration file which is generated on adding ADO.NET Entity Data Model in Entity Framework application, required following changes:
 
  1. Provider invariant name is also specified in the connection string of application (or web) configuration file:
     
    [Existing Code]
     
    <connectionStrings>
    <add name = "NorthwindEntities" connectionString = "metadata=res:///EF.csdl|res:///EF.ssdl|res:///EF.msl;
    provider= System.Data.SqlClient;
    provider connection string= &quot;
    Data Source=QCTEST2;Initial Catalog=Northwind;Integrated
    Security=True;MultipleActiveResultSets=True &quot; " providerName = "System.Data.EntityClient"/>
    </connectionStrings>
     
    Change provider name for our NCache Entity framework Provider and add provider wrapper information in provider connection string:
     
    [Changed Code]
     
    <connectionStrings>
    <add name = "NorthwindEntities"
    connectionString = " metadata=res:///EF.csdl|res:///EF.ssdl|res:///EF.msl; provider= Alachisoft.Integrations.EntityFramework.CachingProvider;
    provider connection string= &quot;
    wrappedProvider=System.Data.SqlClient;
    Data Source=QCTEST2;Initial Catalog=Northwind;Integrated
    Security=True;MultipleActiveResultSets=True &quot;" providerName = "System.Data.EntityClient"/>
    </connectionStrings>
     
     
  2. Also add the provider factory for initialization of NCache Entity framework Provider. The invariant attribute should be the same as the provider name in connection string and SSDL.
     
    <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=1.0.0.0, Culture=neutral, PublicKeyToken=cff5926ed6a53769"/>
    </DbProviderFactories>
     
  3. Following information should be added in appSettings of app.config/web.config :
     
    <appSettings>
    <add key = "app-id" value = "PersonNameApp2"/>
    <add key = "logging-level" value = "Debug"/>
    </appSettings>
 
  • App-id : This will be the identifier for current application. This ID will be used to read configuration from efcaching.ncconf
  • Logging-level : Determines the logging level for application. Our provider will log exceptions and errors only when level is set to Error. When the level is set to Debug, both exceptions/errors and other detailed information will be logged. Nothing will be logged if level is set to Off (default). Path at which log files will be generated: Default = <install-dir>/log-files/efcaching-logs/
  • For applying the above changes, application should be restarted after these changes are made.
 
NOTE: App-id for efcaching.ncconf should be same as in application (app.config) otherwise it will prompt an error)
 
 
Alternative method
 
Apart from specifying connection string in app.config/web.config, you can also specify the connection while creating ObjectContext or EntityConnection. While creating a connection string programmatically, you need to include the wrapper information in a connection string. Here is how you can create connection string and initialize ObjectContext or 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:// /PersonName.csdl|res:// /PersonName.ssdl|res:// /PersonName.msl;" ;
EntityConnection connection = new EntityConnection(efConnBuilder.ToString());
 
For applying these changes, you need to rebuild the application.
 
Step 4: efcaching.ncconf Configurations:
 
Changes required in "efcaching.ncconf".
 
  1. Efcaching.ncconf is placed on the following path: "Installeddirectory:/Program Files/NCache/config".
  2. Provider Configuration contains cache and caching policy related information. Below are the changes required in efcaching.ncconf.
     
    <configuration>
    <app-config app-id = "PersonNameApp" mode = "analysis|caching">
     
  3. "Analysis" mode is used for monitoring the number of times each query executes and then it generates a report. Report contains the Query text and the call count for each query. This report can be used in Custom policy. No caching is done at this mode.
     
    <analysis-policy log-path = "" analysis-time = "60" cache-enable-threshold = "5" default-expiration-type = "absolute|sliding" default-expiration-time = "180" dbsyncdependency = "true"/>
     
  4. Log-path: Path at which analysis log files will be generated.
     
    Default = < install-dir > /log-files/efcaching-analysis-logs/
     
  5. For "Caching" mode, wrapping provider will cache the results of all the specified queries. Caching mode works on either cacheall policy or custom policy. Both policies have their own specifications. Whenever update is detected (either UPDATE, INSERT or DELETE) in a respective database, the provider invalidates affected cache entries by evicting all cached queries which were dependent on any of the updated tables.
     
    <cache-policy default-policy = "cacheall|custom" database = "none|oracle10i2|sqlserver" cache-id = "mycache" enabled = "true">
     
  6. For cacheall policy, it caches all the queries and their results.
     
    <cacheall-policy expiration-type = "absolute|sliding" expiration-time = "1800" dbsyncdependency = "true"/>
     
  7. For custom policy, it includes user-configurable list of queries that should be cached with their results. When custom policy is selected, only those query results will be cached for which the caching is enabled. You can also specify custom caching policy for all queries.
     
    <custom-policy>
    <query query-text = "" ["SELECT FROM Products"] | stored-procedure-name = "" ] caching = "true" expiration-type = "absolute"| sliding = "" expiration-time = "180" dbsyncdependency = "true"/>
    </custom-policy>
    </cache-policy></app-config></configuration>
     
  8. Incase of stored procedures, query text will be the name of a stored procedure, and there will be no default policy or database sync dependency. User can cache stored procedures result with expiration only, no database dependency is required here.
 
See Also
 

 
Copyright © 2005-2012 Alachisoft. All rights reserved.