Purpose of EF 6.x DbContext Generator

I have a web application that I built using Linq-to-SQL and I want to upgrade it to Linq-to-EF. I examined some tutorials and, basically, in the first database scenario, you create an Entity Data ADO.NET data model and from there choose which tables to include in the model (very similar to linq-to-sql). Now in the Add New Item window, I see that there is another option, which is to create an EF 6.x DbContext Generator. enter image description here

What is the purpose of the DbContext Generator compared to creating only the Entity Data ADO.NET data model (the first version of the window) and what is the purpose of the DbContext generator? It seems to be creating a text file; What should I do with it?

+43
c # linq entity-framework
Apr 01 '14 at 4:07
source share
5 answers

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 can also provide you with a graphical visualization of your database, and you need not worry about anything else.

+11
Apr 09 '14 at 8:57
source share

I think this is an important article regarding EntityFramework and generators:

MSDN Article

Here is an introduction to the article:

When you create a model using the Entity Framework constructor, your classes and derived context are automatically generated for you. In addition to generating default code, we also provide several templates that you can use to customize the generated code. These templates are provided as T4 text templates, allowing you to customize templates as needed.

Usually these generators do not convert your db module from LinqToSQL to EntityFramework.
If you have a complete database (suppose you have a Linq2SQL-based module), I would recommend that you use the ADO.NET Entity Data Model (adding a new item: EDMX) and select "Generate from Database" (VS Wizard after adding).

+10
Apr 12
source share

The DbContext generator replaces the ObjectContext with much simpler and shorter code for connecting Entity objects to database objects. A single database table with 30 fields is represented by approximately 800 lines of code as an ObjectContext, but about 40 lines of easily understood code as DbContext and the class generated by DbContextGenerator.

The DbContext generator creates two files -

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

  • creating a class representing each table. If you open these .tt folders, you will see the created DbContext and classes. You do not need to do anything with these classes - you refer to them in controller actions.

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

+10
Apr 13 '14 at 0:02
source share

There are two ways to make a database. Firstly, one includes the EDMX file, the other includes reverse engineering to encode the first POCO.

When you have an EDMX file, you install the generators that are used to create your objects, and there are several options. One of them is DbContext, the other is an EntityObject generator, which generates objects based on ObjectContext.

+7
Apr 01 '14 at 18:15
source share

The first option creates a Model-First file for working with EF (all versions), which, if you choose that you can update (or build) your EF-model from an existing database, and if you select the second option, you will be given the means to creating appropriate files to create a solution for working with Code-First EF.

According to my experience, the second choice is not a worthy option, and if you decide to work with Code-First EF, it is strongly recommended that you open a clean file and write your codes freely and use the maximum flexibility offered by Code-First and their agreements.

+3
Apr 7 '14 at 14:08
source share



All Articles