Following are the prerequisites to enable NCache in Entity Framework application:

1. The user should have an Entity Framework application. The sample application for Entity Framework Caching from NCache sample applications can also be used from the following path: %InstalledDirectory%/samples/clr20/EntityDataModelIntegrationDemo

Change invariant name to EFCachingProviderServices and type to Alachisoft.NCache.Integrations.EntityFramework.EFCachingProviderServices,Alachisoft.Integrations.EntityFramework.CachingProvider as shown below:

The following information should be added in appSettings of app.config or web.config

<appSettings>

<addkey="app-id"value="EFCachingDemo"/>

<addkey="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. NCache.EF Caching provider reads the required information from efcaching.ncconf on the basis of ID given in app/web.config file.

logging-level:Determines the logging level for application. The 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 the level is set to Off (default). Path at which log files will be generated is %Installeddirectory%/log-files/efcaching-logs/

App-id of efcaching.ncconf should be the same as in application (app.config) in order to avoid an error. Application should be restarted after above changes are made.

Alternative method through Programming

Entity Framework 3.5 or 4.0

Apart from specifying connection string in app.config/web.config, the connection can also be specified while creating ObjectContext orEntityConnection. While creating a connection string programmatically, the wrapper information must be included in a connection string. Here is how the connection string can be created andObjectContext orEntityConnection can be initialized.

Apart from specifying database interceptor in app.config/web.config, the database interceptor can be specified while initializing the

configuration (class inheriting DbConfiguration). Here is how the interceptor can be specified.

try

{

this.AddInterceptor(new EFCommandInterceptor());

}

catch (Exception ex)

{

// handle exception

}

Through API (6.0 and 6.1)

User can now also cache results using extension provided for IQueryable.

To utilize the IQueryable interface, include the following namespace in your application: Alachisoft.NCache.Integrations.EntityFramework.Caching.

Call Cache method overloads from IQueryable instance:

var students = from s in db.Students

select s;

students.Cache(); // results are not cached yet

foreach (Student student in students) //results are cached when enumerator from query is iterated completely

{

//using student

}

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

Entity Framework-Caching Configuration File

EFcaching.ncconf configuration contains cache and caching policy related information used by NCache Entity Framework caching provider. Some changes are required in "efcaching.ncconf" to run the Entity Framework Application with NCache. This config file is placed on the following path: %Installeddirectory%/config

This ID is same as defined in app/web configuration file of application.

mode

NCache Entity Framework Caching provider works under two modes. It can either be in "Caching" or in "Analysis" mode.

Analysis Mode

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. No caching is done at this mode.

log-path: The default Path at which analysis log files will be generated is%Installeddirectory%/log-files/EFCachingAnalysisLogs/

analysis-time: Time for which the analysis will run. Default = 1 min

cache-enable-threshold: Caching will automatically be enabled for queries whose call-count exceeds this threshold. By default, its value is 1.

Wrapping provider will cache the results of all the specified queries. Whenever update is detected (either UPDATE, INSERT or DELETE) in a respective database, the provider invalidates the affected cache entries by evicting all cached queries which were dependent on any of the updated tables.

database : Database server where user’s database reside.

cache-id : Specified Cache Name where query results are stored for fast access.

query

Query attribute including user-configurable list of queries that should be cached with their results. A particular query can be configured with its cache policy defined in cache policy tag. Cache query and Cache policy are separated by dedicated tags. User can easily change policies from cache-policy tag without facing any XML parsing error.

cache-query

For cache query, this tag is auto generated by Entity Framework Caching Provider when the application is running in analysis mode. For ease of understanding, commented SQL-query tag is added so that the user can read the query.

cache-query-text : This is used for XML parser and the user is not allowed to make any sort of changes in it.

Cache-policy

When mode is selected as Caching,only those query results will be cached for which caching is enabled. The user can also specify caching policies for all queries.

vary-by-cache-param: User can specify which parameter should be included as part of cache key. NCache Entity Framework Caching Provider will replace the actual parameter in config file with its own names which should follow the following parameter format: @param_[parameter-no/index]

expiration-type: Type of expiration, it can be 'absolute' or 'sliding'. Default is sliding.

expiration-time: Expiration time for this query. Default is 180 sec (3 min).

dbsyncdependency: Determines whether to enable database-dependency for this query or not. Default is false.

SQL Dependency will be registered only for those SQL queries that satisfy all the conditions for SQL Notification Registration as described in the Microsoft provided document.

InProc client cache stores items in object form, therefore if object is modified by the application, it should be updated back to the cache so that other remote clustered cache and other client caches have no data integrity issue.