VS2012 EDMX Defaults Now Align with EF Team Guidance re DbContext Usage

Visual Studio 2010 Defaults were defined Prior to DbContext Release

Visual Studio 2010 and .NET 4 were released before Entity Framework’s DbContext existed. DbContext was first released along with Code First in the Entity Framework 4.1 package.

By default, when you create and EDMX (whether Database First or Model First) in VS2010, the code generator will build classes that inherit from EntityObject and  the context created inherits from ObjectContext.

Back then there was also the option of switching to a T4 template that created lighter weight domain classes from your entities while still using ObjectContet as the base class for the generated context.

Please Generate DbContext, not ObjectContext

Along with DbContext, the team released a new T4 template that would generate even *simpler* domain classesa and a context class that inhertied from the newer, smarter and lighter weight DbContext.

The guidance from the team (and I am in full agreement) was to use this DbContext template for any new project, but stick with the older ones if you needed the backward compatibility. This mean downloading EntityFramework from NuGet and doing the special steps to switch to generating with the T4 template.

New Default Behavior for Visual Studio 2012

Now in Visual Studio 2012, the default behavior follows their guidance! Hooray.

When you create an EDMX, Visual Studio 2012 will automatically use the DbContext template to generate the classes and context *and* it will automatically add EntityFramework.dll to your project.

Here you can see the EntityFramework reference, the two new templates and the packages.config (a NuGet asset which tells the project about the downloaded EntityFramework.dll) that were added to the project when I added a new ADO.NET Entity Data Model item to my project.



  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

7 thoughts on “VS2012 EDMX Defaults Now Align with EF Team Guidance re DbContext Usage

  1. Please note that with this default in place, WCF RIA Services is unable to provide your DbContext models in the Domain Service Class wizard as RIA Services does not have support for DbContext models.

  2. Julie,

    I come back again and again to your blog, and usually find some answers to what I’m trying to solve. One thing- I don’t know where to post a question or comment about a specific topic (hence why I’m posting this at the latest one). Can you give some guidance on that?

    Also, I LOVE working with EF4-5, but I am stuck trying to implement it with a Sharepoint 2010 site (.Net 3.5). Any thought on a best approach? Is there a way to extend the .Net Library classes, such as System.Data.Objects, to add missing methods like CreateObjectSet, or .ContextOptions? I’ve only done this with my own classes. not .Net .dlls

    My latest thought is to use WCF services using .Net 4.0 or better to call methods of the EF4 4.0-4.5 project.

    Thanks for listening (I saw some of your older articles expressing similar pain)

  3. Hi Steve,

    On my contact page there is a form, but it is prefaced with a plea to try resources like StackOverflow etc since I get so many emails from confused EF users. 🙂

    I have managed to stay far away from Sharepoint for a long time. I did do a little googlebinging on this but I came up with bupkis (yes I had to go figure out how to spell that!) So I don’t think I’ll be able to help you with this one. Sorry 🙁

  4. Uh-oh; I feel like I just came across the Death Star hovering over my home planet. I wish I had the option of bailing, but it’s my task to complete. Looks like an SOA approach is the only way to go. I hope SP 2013 is at least on 4.0. Thanks!

  5. Hi Steve,

    I had the same your problem last year and eventually I developed a datacentric architecture using WCF 4.0 and Self tracking entities (STE) in order to use their capabilities to track entity state and make CRUD operations in a simple way.

    At the beginning I was searching a way to serialize lambda expression used in queries in order to reduce to the minimum the role of service layer and keep it for other projects. Unfortunely I have developed it only for simple lambda expressions and for particular cases I have created custom methods in service contract.

    However my suggestion it is to use WCF implementing your queries that returns STE objects and a generic method to save/update/delete them using EF ApplyChanges method.

    I hope I’ve been of some help



  6. I was very happy to see this change in VS2012, great stuff!

    Is it possible in VS2012 to have the data annotations (Max Length, etc) defined in the EDMX to flow down to the POCOs? Or do I have to continue to use the "ugly buddy class" kludge you describe in your dbContext book?

    Personally I prefer to not use the edmx files at all and go Code Only, but my coworkers want to use the GUI.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.