How To Configure Entity Framework Caching
How To Configure Entity Framework Caching
Integration Modes:
NCache Entity Framework Caching provider works under two modes. It can either be in "Caching" or in "Analysis" mode. In caching mode you can cache result-set of selected queries or you can have a CacheAll option where result of all SELECT queries will be cached automatically. Analysis mode works in pass-through mode and helps you find the queries that you should cache by generating a report showing which queries are being called with what frequency. For more help on Integration Modes
Database Synchronization:
NCache Entity Framework provider also ensures that data in cache is always synchronized with the database. Therefore NCache uses .NET SqlCacheDependeny which registers a SQL query with SQL Server so if any row in the dataset represented by this query is changed in the database, SQL Server throws an event notification to NCache. NCache then removes the corresponding result-set from the cache.
SSDL Application (or web) configuration file 2. Microsoft Visual Studio 2008 with SP1. 3. Database Tool (e.g. MS SQL SERVER 2005, ORACLE)
[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 theProviderManifestToken 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">
[Existing Code]
<connectionStrings> <add name = "NorthwindEntities" connectionString = "metadata=res:///EF.csdl|res:///EF.ssdl|res:///EF.msl; provider= System.Data.SqlClient; provider connection string= " Data Source=QCTEST2;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True " "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= " wrappedProvider=System.Data.SqlClient; Data Source=QCTEST2;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True ""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"/>
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>
1. App-id : This will be the identifier for current application. This ID will be used to read configuration from efcaching.ncconf 2. Logging-level : Determine 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/ 3. 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)