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

45,006

Solution 1

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.

Solution 2

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

Solution 3

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).

Solution 4

There are two ways to do DB First, one involves an EDMX file, the other involves reverse engineering to code first POCO's.

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

Solution 5

The first option create a Model-First file to work with EF (all versions) which if you choose that you can update (or construct) your EF-Model from an existing database while if choose second option, you would be given facilities to generate corresponded files to build a solution for working with Code-First EF.

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

Share:
45,006
frenchie
Author by

frenchie

Started programming when I was 10. Never made it my profession but always programmed, mostly with Excel VBA for finance, and for some simple front-end web design in the early 2000's (ie. no jquery back then, what a pain it was with DHTML). One day, I get the idea of outsourcing the web development of my start-up: total cacacacacacacafukkkk! So: from VBA to C# .NET and Javascript in 5,000 pages of programming books, and lots of Stackoverflow! I think asking dumb questions is a smart way to learn. And as an internet entrepreneur, I've learned that what matters most is not knowing how to program but knowing what to program: discover the goyaPhone at www.goyaphone.eu

Updated on July 05, 2022

Comments

  • frenchie
    frenchie almost 2 years

    I have a web app that I built using LINQ to SQL and I'm looking to upgrade it to LINQ to Entity Framework. I've looked at some tutorials and what I've learned is that basically in the database-first scenario, you create an ADO.NET Entity Data Model. And from there, you select which tables to include in the model (very similar to LINQ to SQL).

    Within the Add New Item dialog, I see that there is another option that creates an EF 6.x DbContext Generator:

    Visual Studio Add New Item dialog

    What is the purpose of EF 6.x DbContext Generator compared to ADO.NET Entity Data Model (first option in dialog)? And, what is EF 6.x DbContext Generator for? It seems to create a text file. What should I do with it?

  • frenchie
    frenchie about 10 years
    What's the difference between DbContext and EntityObject generator?
  • Erik Funkenbusch
    Erik Funkenbusch about 10 years
    @frenchie - I stated quite clearly, one is based on DbContext, the other is based on ObjectContext.
  • JMarsch
    JMarsch about 10 years
    @frenchie If you are asking the difference between DbContext and ObjectContext, they are quite different. DbContext is the newer of the two, and is the one that allows you to use "Plain old C# Objects". The reason it is referred to as "DbContext" is because the Context object (sort of like dataset) is a DbContext class and not an ObjectContext class. Most of the feature demos that you see with EntityFramework involve DbContext.
  • Joyce
    Joyce about 9 years
    Is exposing entity as datacontract still an issue for EF6.x?
  • Tim Sparkles
    Tim Sparkles almost 6 years
    This answer would be improved by referring to "the first option" by name instead of ordinally.
  • Marc.2377
    Marc.2377 over 5 years
    This answer does not explain the purpose of one compared to the other, like it was asked :(
  • brduca
    brduca over 5 years
    @Marc.2377 back in 2014 (when this question was made), Microsoft has a confusing behavior in Entity Framework and LinqToSQL approach. This does not apply now a day. Down voting answers, instead of providing a better one is not productive at all.
  • Marc.2377
    Marc.2377 over 5 years
    @brduca Not sure if you are taking downvotes personally, but in case you are, I would advise against it.
  • brduca
    brduca over 5 years
    @Marc.2377 I really can't see the point of downvoting a 5 year old answer just because... The question was about 2 very similar technologies, that overlaps in several aspects. It was marked as answered and given bounty points (so it helped who made the question, clearly). If you really want to be helpful write a better answer yourself.
  • mins
    mins about 5 years
    I think both options can create a derived DbContext, For the first one "ADO.NET Entity Data Model, you can select in the wizard the sub-option "Code First from Database" which creates entities and the derived DbContext to access them. See this. Maybe that was not the case in 2014, but for today the question seems unanswered.
  • Yusha
    Yusha almost 5 years
    The answer does not answer the OPs question. The question is: "What is the purpose of EF 6.x DbContext Generator compared to ADO.NET Entity Data Model (first option in dialog)? And, what is EF 6.x DbContext Generator for?". The answer doesn't have anything to do with the original question - hence the down votes. It's like someone saying "What benefits does drinking Water over drinking Soda give you?". Then someone replying "Soda is a good source of caffeine". Thanks, but that doesn't really help. Just my two cents.
  • brduca
    brduca almost 5 years
    @Yusha I believe that this questions should be put in perspective and the entire questions must be read but the title. If you know more about this technology feel free to provide a more in depth explanation. By the way, if you take the time to scroll down you'll see it already there. Useless downvote.