Watch out for VS2012 EDMX Code Generation Special Case

There’s a known problem with EDMX code generation in VS2012. I’ve tested this a number of ways in VS2010 and can’t reproduce the problem there so it seems to be specific to VS2012.

The problem occurs when you have your model inside of a project folder such as in this screen shot:

image

When you make changes to your model, normally, saving the model will trigger the code generator to regenerate the classes.

But when the model is inside a folder, this won’t happen. (It’s not intentional…just a bug.) And you may not realize that this is the problem that’s causing you to have problems during development. Instead you won’t be able to use new (or modified) entities or properties when you are coding against the model because the generated classes weren’t updated.

So, for example, if you change an entity from Contact to Person, (and the EntitySet name will then change from Contacts to People), when you try to write a query against context.People, “People” will not exist as an option. Or if you try to write code related to Person, there will be no such class available.

This is a known issue and has been logged as an item for Entity Framework on the CodePlex site. You can vote for it to raise it’s priority level here: http://entityframework.codeplex.com/workitem/453.

In the meantime, if you want to keep your edmx file inside a project folder, you can still force the code generation to happen by selecting Run Custom Tool from the context menu of the Context.tt file and the model.tt file. Be sure to update BOTH files so that any entity name changes will get picked up by the context as well.

SNAGHTMLf6e3b4b

Thanks to Cori Drew who brought this problem to my attention and to Pawel Kadluczka from the EF team for pointing me to the work item when I had given up on a web search.

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

10 thoughts on “Watch out for VS2012 EDMX Code Generation Special Case

  1. Thanks for posting so I know I wasn’t the only one experiencing this annoying issues.

    To save a couple clicks you can go to BUILD > Transform All T4 Templates.

  2. There is actually another workaround option. I discovered this error because I upgraded an EF 4 EDMX to EF 5 by adding `EF 5.x DbContext Generator` to the project, which added T4 templates to the existing EDMX.

    However, in the project file it did not mark them as `DependentUpon` the EDMX file so they did not appear under the EDMX node. Instead they appeared side-by-side with the EDMX in the folder. T4 generation worked fine until I manually went in and added the `DependentUpon` nodes in the project file.

    Observe: http://www.youtube.com/…/watch

  3. Arrggg this has been driving me crazy all afternoon! I thought I was losing my mind. 😀

    I fixed it by using ObjectContext instead of dbcontext.

    But now I know the real reason.

    Thanks. 🙂

Leave a Reply

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


The reCAPTCHA verification period has expired. Please reload the page.

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