Entity Framework is a tried and tested Microsoft object-relational mapping framework for .NET. It enables you to work with relational data through domain objects. And, it eliminates the need for most of the database persistence code that you would otherwise need to write.
Entity Framework is used in high-transaction applications where performance and scalability are critical. But, in most of these applications, the database quickly becomes a bottleneck. As, unlike the application tier, where you can add more application servers as you need to scale, you cannot do the same with the database tier.
The only way to achieve this scalability is with distributed cache like NCache. It is an extremely fast and scalable distributed cache for .NET applications. It lets you cache application data, reduce expensive database trips, and improves your application performance and scalability.
Although, Entity Framework does not provide a Second-level (2nd Level) Cache provider model. NCache has implemented a custom ADO.NET provider for Entity Framework with which, it can plug-in and cache the results of Entity Framework queries. This allows you to start using distributed cache in your Entity Framework application without making any code changes.
If your Entity Framework application is running in a multi-server environment, then you need a distributed cache as an Entity Framework Second Level Cache. And, NCache is an ideal option for multi-server configurations, and you should use it for the following reasons:
The best thing about using NCache with Entity Framework is that there is no programming required on your part. You simply modify your application's configuration files by adding an interceptor in the <provider> section and overwriting the invariantName and type configurations. Here is an example of app.config changes:
<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>
The NCache Entity Framework Second-level Cache provider plugs in as a custom ADO.NET provider. This provider allows you to run in analysis mode, where it logs all the different queries your application issues to Entity Framework. These queries are logged in a file along with a count of how many times this query was called during this analysis mode. Below is an example:
<analysis-report>
<query>
<!--Call count = 12-->
<cache-query query-text="SELECT [Extent1].[ProductID] AS [ProductID],
[Extent1].[ProductName] AS [ProductName],
[Extent1].[SupplierID] AS [SupplierID],
[Extent1].[CategoryID] AS [CategoryID],
[Extent1].[QuantityPerUnit] AS [QuantityPerUnit],
[Extent1].[UnitPrice] AS [UnitPrice],
[Extent1].[UnitsInStock] AS [UnitsInStock],
[Extent1].[UnitsOnOrder] AS [UnitsOnOrder],
[Extent1].[ReorderLevel] AS [ReorderLevel],
[Extent1].[Discontinued] AS [Discontinued] FROM
[dbo].[Products] AS [Extent1]
WHERE 5 = [Extent1].[ProductID]"/>
<cache-policy vary-by-cache-param=""
expiration-type="Absolute"
enabled="True"
expiration-time="30sec"
dbsyncdependency="False"
resyncProviderName="EFResync"/>
</query>
</analysis-report>
This allows you to determine which queries you want to cache and for how long. You can also specify database synchronization for some or all of your queries.
By plugging in NCache Entity Framework Second-level Cache provider through app.config changes, you are going to gain an enterprise-level distributed cache for your application. Following are some features NCache provides for Entity Framework apps.
Although, using NCache as Entity Framework Second Level Cache is very quick and easy, it only provides you with a limited set of NCache features. The reason for this is that NCache is bound by the fact that it is plugged in as a custom ADO.NET provider and must deal at the SQL query level. There are numerous NCache features that you are unable to use if you decide to use it as a Entity Framework Second-level Cache Provider.
The alternative for you is to use NCache directly from within your Entity Framework application by making NCache API calls like all other non-Entity Framework .NET applications do. Although, there is a small programming effort to take this path, you might decide that the benefits outweigh the extra programming effort.
Below is a list of features you would miss if you used it as Entity Framework Second Level Cache versus if you used NCache directly from your application.
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.