Google+

Monday, August 26, 2013

4 Step-By-Step How-to add a DBContext code generator

            By Carmel Schvartzman

 In this tutorial we'll add an DBContext API to the .NET MVC 4 application which serves as a backend to our Blogs Application. You can follow this step by step tutorial as a standalone, or you can learn all the posts to build the entire app. This is the STEP 4 in this walkthrough to design an end-to-end AngularJS client MVC app.

The advantage of generating POCO (Plain Old CLR Objects) classes via T4 templates such as the ADO.Net DBContext code Generator is that they offer extensibility over the entity object definitions, far over standard EDMX data access. Also the DbContext can be used with Database First, Model First and Code First programming development.
There is not performance impact, and the DbContext API is simplified and user friendly, both in terms of features and usage. However, we can still getthe ObjectContext functionality from the DbContext and use features available only in the former. Most important, in this tutorial we'll be using the Data First approach, so we'll want to implement the DBContext.

1. Open the .EDMX file we created in STEP 3, open the MODEL BROWSER , Mouse over the Conceptual Entity Model and right click, selecting "Add Code Generation Item":

2. In the dialog, type "DBCONTEXT" on the SEARCH text box. After a little while, select the EF 5.x DbContext Generator, and type "NatureModel.tt" as the template name:

Press the "ADD" button.
3. Note the files added to the Models folder:

4. Take a close look at the Blog.cs file:

This is the POCO class that the code generation tool created for us.

5. In the POCO BLOG class, take a look at the public properties. You'll see that a "Comments" collection has been added, representing the one-to-many Blog-Comments relationship:
The "virtual" keyword controls lazy loading, that means if you use the "virtual" keyword on an ICollection one-to-many relationship property, it will be lazy-loaded by default, instead if you don't, it will be eager-loaded. The EntityFramework will create at runtime a new class derived from the "Blog" class and use it instead. This dynamically created Blog classwill contain logic to load the navigation property data when accessed for the first time, provided that all the class' navigation properties are virtual, the context.Configuration.ProxyCreationEnabled must not be disabled, the context.Configuration.LazyLoadingEnabled must not be disabled,  and also the entity must be attached  to the context.

Lazy loading means the process whereby a collection of entities is automatically loaded from the database just the first time that the property referring to the entities is accessed.  For example, when using the Blog  class , the related Comments wont be loaded until the first time the Comments navigation property is accessed. Lazy loading of the Comments collection can be turned off by making the Comments property non-virtual. Also, lazy loading can be turned off for all the entities in the context.

6. The same considerations apply on the "Comment" entity:

That's all!! Happy programming.....
כתב: כרמל שוורצמן

2 comments: