Click here to Skip to main content
Licence CPOL
First Posted 8 Dec 2010
Views 5,779
Bookmarked 0 times

EF Feature CTP5 – Walkthrough For The New DbContext T4 Template

By | 8 Dec 2010 | Technical Blog
EF Feature CTP5 – Walkthrough For The New DbContext T4 Template One of the new features in the EF Feature CTP5 that was released yesterday was a new T4 template for generating DbContext instead of ObjectContext. In this post I’m going to explain what is the new DbContext and then show how to use th
A Technical Blog article. View original blog here.[^]

One of the new features in the EF Feature CTP5 that was released yesterday was a new T4 template for generating DbContextinstead of ObjectContext In this post I’m going to explain what is the new DbContextand then show how to use the new T4 template. Pay attention that the details I provide might change in the future since its only a CTP and not a release.

What is DbContext?

The DbContextis a new lightweight context that was created and provided within the EF Feature package. The DbContextis a wrapper for the ObjectContext(it doesn’t inherit from it). Since not in all development scenarios we need the full feature list of the ObjectContext the DbContextcan help us by exposing only the common functionality that we need. It includes some additional features for our disposal such as OnModelCreatingextension point.

Using DbContext T4 Template

In order to use the new T4 template for DbContextyou start with your model. The model can be created with the Database First approach or with the Model First approach. The model I’m going to use is the following:

Entity Designer Diagram

When you want to use the T4 template, from the designer surface you will press the right mouse button and press the Add Code Generation Item menu item.

Add Code Generation Item

From the Add New Item window choose the new ADO.NET DbContext Generator and give the model an appropriate name:

Add New Item

After pressing the Add button two new files will be created and a reference to the new EntityFramework.dll will be added:

Solution Explorer

The first file (SchoolModel.Context.tt) will contain the DbContextand the second file (SchoolModel.tt) will contain the entities. Now you can start coding against the DbContext In the following example I query the database using the DbContext to find the department with id of 1:

class Program
{
  static void Main(string[] args)
  {
    using (SchoolEntities context = new SchoolEntities())
    {
      var query = context.Departments.Find(1);
      Console.WriteLine("{0} has a DepartmentID of 1", query.Name);
    }
  }
}

The result of the query:

Result

Summary

You can expect that the EF ecosystem will continue to grow. The new feature CTP adds more functionality for our disposal. One of these features is the new DbContextand also its new T4 template.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gil Fink

Architect
Sela Group
Israel Israel

Member

Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThis was more helpful than any Microsoft link... Pinmembergodbrain7:46 12 May '11  
GeneralRe: This was more helpful than any Microsoft link... PinmemberGil Fink8:14 12 May '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 8 Dec 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid