The Entity Framework is used as a persistence framework in Kooboo, but since EF1.0 does not provide a full cache solution, it has long been a problem for data caching, and has taken a temporary workaround without finding a suitable solution: to cache entities directly. However, because entity entities are stateful and have indirect inverse references to ObjectContext, caching of entities with State can cause confusion in the object context and inability to properly release the connected resources. As a result, the cached entity entities must first be detached or redefined by the Poco entity as the cache object instead of the entity entity. As a result, the association of all the cached entities is invalidated, causing problems with the use and serious deficiencies in the entire software framework.
Besides, the SQL log problem of EF. In the previous LINQ to SQL Project, there was a visual debugger that could look at the query expression to generate the corresponding SQL statement, which would greatly facilitate the developer's debugging efforts. Can be found in EF1.0, but it has not been possible to find similar tools available. Therefore, my practice is to view the SQL statements generated and executed by EF through SQL profile. Although it is feasible, it is still very inconvenient.
Now, the EF team has finally launched a more complete solution for caching and SQL execution logs, efproviderwrappers. They do this by adding a layer of packaging on top of the original EF provider, which intercepts the data cache and log monitoring. The cached data here is the native data returned after the database query, not the entity entities object, which avoids the extreme negative effects of the entity state on the cache. And such a cache on the upper layer of the data query itself is transparent, in the same closed interval, the cached data depends on the entity type after being updated (the corresponding table has a curd operation), the cache and will be automatically emptied. For the monitoring of the log, after this layer of packaging can be very easy to get processing.
Although the above figure is valid for SqlClient, the provider of the different data should be valid because the layer wrapper does not involve specific SQL operations. Here's a quick example of how to use it.
In the download of the Efproviderwrappers solution, the efproviderwrappertoolkit,efcachingprovider,eftracingprovider of these three projects is the real officer, Other projects are example projects. In the Efproviderwrapperdemo project, we can find the example we want.
The first step: Add the following configuration to the configuration file:
Hide Line number copy code ?
-
< System.Data >
-
<dbproviderfactories>
-
< add name = " ef Caching Data Provider "invariant = " Efcachingprovider "description = " caching Provider Wrapper "type = "efcachingprovider.efcachingproviderfactory, Efcachingprovider, version=1.0.0.0, culture=neutral, publickeytoken=def642f226e0e59b " />
-
< add Name = "ef tracing Data Provider " invariant = " Eftracingprovider "description = " tracing Provider Wrapper "type = "eftracingprovider.eftracingproviderfactory, Eftracingprovider, version=1.0.0.0, culture=neutral, publickeytoken=def642f226e0e59b " />
-
<name="EF Generic Provider Wrapperinvariant=" Efproviderwrapperdescription="Generic Provider Wrappertype= "efproviderwrappertoolkit.efproviderwrapperfactory, Efproviderwrappertoolkit, version=1.0.0.0, Culture =neutral, publickeytoken=def642f226e0e59b/>
-
</dbproviderfactories>
-
</system.data>
-
The second step is to inherit an extended ObjectContext from the generated ObjectContext, defining the required extended properties, which focus on redefining the constructor and generating the wrapped EntityConnection object. Note name refers to the names of database connections in Web. config
Hide Line number copy code ?
-
NorthwindEntities
-
{
-
Logoutput;
-
-
Extendednorthwindentities ()
-
This ("Name=northwindentities")
-
{
-
}
-
-
Extendednorthwindentities (connectionString)
-
Base (entityconnectionwrapperutils. Createentityconnectionwithwrappers (
-
ConnectionString,
-
"Eftracingprovider",
-
"Efcachingprovider"
-
))
-
{
-
}
-
Step three: Specify the cache manager and cache policy, and the cache manager is extensible. The default provides two cache manager implementations, Inmemorycache and Aspnetcache, plus a distributed cache nvelocity adapter implementation: Velocitycache. For log monitoring, you can set whether output to the console or output to a file via Eftracingproviderconfiguration, and, of course, the extended ObjectContext can also output the log to the specified TextWriter:
Hide Line number copy code ?
-
Application_Start (e)
-
{
-
eftracingproviderconfiguration. Logtofile = Server.MapPath (string. Format ("{0:yyyy-mm-dd}"DateTime". txt "
-
Efcachingproviderconfigurationaspnetcache();
-
efcachingproviderconfiguration Cachingpolicy. Cacheall;
-
}
-
You can then use it directly, noting that different ObjectContext instances point to the same Icache instance, and that even if your objectcotnext does not require a cache operation, you should specify the Icache instance and set the cache policy to nocaching. This will automatically empty the cached data of the updated entity type when your ObjectContext calls the SaveChanges.
"Cache" Ef4providerwrappers