DCSIMG
EF Feature CTP5 – Walkthrough For The New DbContext T4 Template - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

EF Feature CTP5 – Walkthrough For The New DbContext T4 Template

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 forEF Feature CTP5 – New T4 Template for DbContext generating DbContext instead of ObjectContext. In this post I’m going to explain what is the new DbContext and 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 DbContext is a new lightweight context that was created and provided within the EF Feature package. The DbContext is 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 DbContext can help us by exposing only the common functionality that we need. It includes some additional features for our disposal such as OnModelCreating extension point.

Using DbContext T4 Template

In order to use the new T4 template for DbContext you 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 DbContext and 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 DbContext and also its new T4 template.

Comments

VNX » Blog Archive » Entity Framework Feature CTP5 Released with examples said:

Pingback from  VNX  » Blog Archive   » Entity Framework Feature CTP5 Released with examples

# December 9, 2010 11:28 AM

The Morning Brew - Chris Alcock » The Morning Brew #747 said:

Pingback from  The Morning Brew - Chris Alcock  » The Morning Brew #747

# December 9, 2010 12:55 PM

Gil Fink on .Net said:

EF Feature CTP5: Raw SQL Query/Command Support One of the new features that EF feature CTP5 supplies

# December 10, 2010 1:34 PM

Modifying an entity using T4 Template DBContext in Entity Framework 4.1 | John Parr said:

Pingback from  Modifying an entity using T4 Template DBContext in Entity Framework 4.1  | John Parr

# November 2, 2011 3:54 PM

Can the VS Entity Data Model Designer be configured to use a newer version of the EF? | PHP Developer Resource said:

Pingback from  Can the VS Entity Data Model Designer be configured to use a newer version of the EF? | PHP Developer Resource

# May 24, 2012 9:41 AM