Purpose of EF 6.x DbContext Generator option when adding a new data item in Visual Studio Purpose of EF 6.x DbContext Generator option when adding a new data item in Visual Studio asp.net asp.net

Purpose of EF 6.x DbContext Generator option when adding a new data item in Visual Studio


The DbContext Generator replaces the ObjectContext with much simpler and shorter code to connect Entity objects to database objects. A single database table with 30 fields is represented by about 800 lines of code as an ObjectContext but about 40 lines of easy to understand code as a DbContext and class generated by the DbContextGenerator.

The DbContext Generator creates two files -

  1. creating the DbContext with connection string details and a DbSet for each table.

  2. creating the class representing each table. If you open these .tt folders you will see the DbContext and classes generated. You don't need to do anything with these classes - you refer to them in the Controller actions.

A walkthrough is available at http://msdn.microsoft.com/en-US/data/jj206878


I think this is the essential article relating to EntityFramework and generators:

MSDN Article

Here is the introduction to the article:

When you create a model using the Entity Framework Designer your classes and derived context are automatically generated for you. In addition to the default code generation we also provide a number of templates that can be used to customize the code that gets generated. These templates are provided as T4 Text Templates, allowing you to customize the templates if needed.

Generally these generators do not convert your db module from LinqToSQL to EntityFramework.
If you have complete database (suppose you have it as a module based on Linq2SQL) I'd recommend you to use ADO.NET Entity Data Model (adding new item: EDMX) and choose 'Generate from database' (VS Wizard after adding).


If you already have a database, the first option is better, given that the approach is very similar to the one you are already working in LinqToSQL. The .EDMX file also can provide you a graphical visualization of your database, and you don't have to worry about anything else.