Pre-Generated Views With a Code First Model Entity Framework 6.0

Introduction

Entity Framework generates a set of mapping views to access the database before executing any queries or save changes to the data source. These mapping views are a set of E-SQL (Entity SQL) statements that represent the database in an abstract manner. Mapping views are cached per application domain. It means that if we create multiple instances of the same context in the same application domain, EF reuses the mapping views from the cached metadata. Entity Framework generates the mapping view per application domain when executing the first query and it will take a significant part of time to execute. Entity Framework enables us to pre-generate mapping views and include them in a compiled project.

Generating Mapping Views using EF Power Tool

Entity Framework Power Tool is one of the easiest ways to pre-generate a view. Once we have installed the EF Power Tool, we may be able to see the menu option to generate the view on a right-click of the DbContext class (Code First) / Edmx file (Database First).

Entity Framework Power Tools download link: Entity Framework Power Tools Beta 4.

generate view

Once the process is finished, we will have the following class generated.

generated class

From now on when the application is run, Entity Framework will use this pre-generated whatever to load the views that are required. This class must be re-generated whenever any changes are made to the model otherwise Ef will throw an exception.

Generating Mapping Views from Code (EF6 or later version)

Entity Framework version 6 has introduced an API to generate a view. When we use this method we have the freedom to serialize the view but we need to load the views by ourself.

The System.Data.Entity.Core.Mapping.StorageMappingItemCollection class represents a collection of items in Storage Mapping. The ObjectContext has all the information about the storage mapping collection and we can retrieve it using the metadata workspace of ObjectContext. Generally the developer uses the DbContext class to access the database and the DbContext class implements the IObjectContextAdapter interface to provide the access to the underlying ObjectContext.

class

By using the following code we can get a storage mapping collection.

  1. var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;  
  2. var mappingCollection = (StorageMappingItemCollection)objectContext.MetadataWorkspace .GetItemCollection(DataSpace.CSSpace);  
Once we have the Storage Mapping Item collection, we can access the methods of GenerateViews and ComputeMappingHashValue of the StorageMappingItemCollection class.

The GenerateViews method creates a dictionary of entities for each view in the container mapping and the ComputeMappingHashValue method computes the hash value for a single container mapping and it is very useful at runtime to identify whetehr there is no change in the model after generating views.

The DbMappingViewCache class has one property, MappingHashValue, and one method, GetView. MappingHashValue must returned as generated by the ComputeMappingHashValue method. When the Entity Framework loads the view, it will first compare the hash value of the model and the hash returned by this property. If they do not match then the Entity Framework throws an exception type of EntityCommandCompilationException.

The GetView method accepts an EntitySetBase and returns a DbMappingVIew containing the Entity SQL that was generated for that that was associated with the given EntitySetBase in the dictionary generated by the GenerateViews method. The GetView method returns null when we do not have the required view.
  1. public class MyMappingViewCache : DbMappingViewCache  
  2. {  
  3.     public override string MappingHashValue  
  4.     {  
  5.        get { return "df5a56995eb89ee1d0f7cd1c0bd989a32f6f6c500f524f64aba8f87706ab51ab"; }  
  6.     }  
  7.   
  8.     public override DbMappingView GetView(EntitySetBase extent)  
  9.     {  
  10.         if (extent == null)  
  11.         {  
  12.             throw new ArgumentNullException("extent");  
  13.         }  
  14.   
  15.         var extentName = extent.EntityContainer.Name + "." + extent.Name;  
  16.   
  17.         if (extentName == "CodeFirstDatabase.Employee")  
  18.         {  
  19.             return GetView0();  
  20.         }  
  21.   
  22.         if (extentName == "Model.Employees")  
  23.         {  
  24.             return GetView1();  
  25.         }  
  26.   
  27.         return null;  
  28.     }  
  29.   
  30.    private static DbMappingView GetView0()  
  31.     {  
  32.         return new DbMappingView(@"  SELECT VALUE --Constructing Employee [CodeFirstDatabaseSchema.Employee] 
  33.         T1.Employee_Id, T1.Employee_Code, T1.Employee_Name)  
  34.         FROM (  
  35.         SELECT   
  36.            T.Id AS Employee_Id,   
  37.            T.Code AS Employee_Code,   
  38.            T.Name AS Employee_Name,   
  39.            True AS _from0  
  40.         FROM Model.Employees AS T  
  41.             ) AS T1");  
  42.     }  
  43.   
  44.     private static DbMappingView GetView1()  
  45.     {  
  46.         return new DbMappingView(@"  SELECT VALUE -- Constructing Employees  [PreGeneratedView.Model.Employee]
  47.         (T1.Employee_Id, T1.Employee_Code, T1.Employee_Name)  
  48.          FROM (  
  49.          SELECT   
  50.            T.Id AS Employee_Id,   
  51.            T.Code AS Employee_Code,   
  52.            T.Name AS Employee_Name,   
  53.            True AS _from0  
  54.        FROM CodeFirstDatabase.Employee AS T  
  55.        ) AS T1");  
  56.     }  
  57. }  
Summary

Using the method described previously we can create a pre-generated view and using the pre-generated view we can improve the performance of the Entity Framework (first time query execution).

Up Next
    Ebook Download
    View all

    My Ideas

    Read by 2 people
    Download Now!
    Learn
    View all