Entity Framework 6 and Model/Database First Work Flows

Visual Studio 2012 (out-of-band release) and Visual Studio 2013 (in-box) now support Model/Database workflows for EF6. See this post for more details.

Entity Framework 6 Alpha 2 has shipped. It has some new cool features (like custom conventions or automatic discovery of entity configurations) and a few other improvements (like improved queries for Linq Enumerable.Contains or changing the default isolation level when creating a SqlServer new database with CodeFirst). Most of the new features and many improvements are CodeFirst related or CodeFirst only. Still there are people who would prefer using a designer to draw a model and create the database or create a model from an existing database and tweak it. The latest version of the Entity Framework Designer which shipped in VS2012 supports only EF5 so it does not seem like it could handle EF6. However after seeing a question on the Entity Framework team blog a couple days ago I thought it would be interesting to really see if this is the case and what it would take to be able to actually use ModelFirst and DatabaseFirst work flows with EF6. In general I thought it might be possible – artifacts have not changed since EF5. As well as most APIs. The two fundamental changes to EF6 are changes to the provider model and all the “new” types that previously lived in System.Data.Entity.dll. New provider model should not be a big concern here – we care about the results here and not about how they are achieved. So, as long as the designer is able to create the database correctly (model first approach) or the edmx file from the database (database first approach) EF6 runtime should be able to use those. Changes to types seemed more worrisome – not only types themselves changed and have new identities but in many cases namespaces changed as well. Luckily the designer now uses T4 templates to generate the code from the edmx file so it is just a pure textual transformation. I expected that I would need to change the T4 templates a bit to make the code compile with EF6 but it should be easy. After all this mental work-out I decided to try it out. I opened VS 2012, created a new project, added a new ADO.NET Entity Framework model, removed references to System.Data.Entity.dll and EntityFramework.dll (5.0.0.0) and added a reference to the EF6 Alpha2 package using NuGet. Then I created a model from an existing database. The project compiled without errors. I added a few lines of code to bring some entities from the database and everything worked. Adding new entities worked as well. Finally I deleted my model and created a new model to try the Model First approach. Similarly I had to remove references to System.Data.Entity.dll and EntityFramework.dll (5.0.0.0) but other than that everything just worked. While what I did was not a very comprehensive test and using VS2012 for EF6 projects is in general not supported I am pretty confident it will work and should be sufficient until a version of the designer that supports EF6 ships.
(Yes, I am a bit disappointed with how easy it was. I hoped this would be a report from a battlefield where I was able to achieve my goal by using a hack here or adding a few lines of code there and maybe even producing a VSIX as a side effect. On the other hand I am happy that even though the post is a little boring the experience for users is much nicer. This is more important).

5 thoughts on “Entity Framework 6 and Model/Database First Work Flows

    1. It’s not clear to me what you are referring to – the post I wrote or the post on the SO. I have not tried to map to stored procedures with the VS2012 designer for EF6 but I don’t see why it should not work. For model first and database first flows it worked in EF5 and should continue to work in EF6. EF5 CodFirst does not support mapping to stored procedures but it’s coming to EF6. Also, EF6 enabled EFDesigner should be released soon. Stay tuned.
      If you are referring to the SO thread it appears to me that you are using MVC. AFAIK MVC does not support EF6 yet but again from what I know work is in progress. So, if you are using MVC you will need to hold on with EF6 for a while.

      Like

      1. Pawel,

        Thanks for the reply.

        I’m not using Code First. I use the Database First approach.

        Yes, I have an ASP.NET MVC 4 app that makes use of a class library where I have the DbContext and POCO classes defined.

        The problem I got was related to ASP.NET built in Membership functionality as you can see in this comment I made at that SO question: http://stackoverflow.com/questions/14106286/ef-6-system-data-objects-objectcontext-error/14114136#comment20278900_14114136

        I get that same error described in the SO question when a call to Membership.GetUser(model.UserName) is made inside the Login action method in the AccountController. As hvd said in this comment: http://stackoverflow.com/questions/14106286/ef-6-system-data-objects-objectcontext-error/14114136#comment19559479_14114136 it’s not possible to use EF 6 right now because the ASP.NET provider (Membership) still depends on EF 5.

        I’m really looking forward to EF 6 and EFDesigner support in Visual Studio 2012. EF 6 async functionality is awesome!

        As of know I rolled back to EF 5 as it’s working great.

        Keep rocking and making EF the awesome software product it is.

        God bless,

        Leniel

        Like

        1. Thanks for your kind words Leniel. We are working on the EF Designer – the plan is that it will support both EF5 and EF6 on VS2012. Moreover we are trying to move it to the open source model similarly to what we did with the runtime bits.

          Like

Leave a comment