Piggy69
Piggy69

Reputation: 85

Upgrade from EF 4.0 ObjectContext to EF 6 / 6.1

I would like to upgrade a very large project based on FX 4.0 and EF 4.0 (Database First with ObjectContext) to the latest 6/6.1 version of EF.

I have already completed (successfully) all the steps to actually upgrade the EDMX (adding T4 template, changing the namespace references and so on following this post Upgrade EF 4 EDMX to EF 6), and now my data-access assembly is compiling fine.

In order to get the things done quickly, I used the ObjectContext T4 template to execute the upgrade in the hope of minimizing the required code changes. The template I used is the "EF 6.x EntityObject Generator template" from Visual Studio Gallery and the entire solution will still base on the .Net Framework 4.0.

I would like to know if using the ObjectContext instead of the DBContext with EF6 will reduce or limit the benefits of the new EF 6 (I will continue using the Database First strategy).

Upvotes: 2

Views: 1933

Answers (2)

t3z
t3z

Reputation: 1757

DbContext is just a (good one) wrapper around ObjectContext that enables fairly easy Code First which allows you to use POCO's easily and generally it is more convenient to build a Data Layer with it. But essentially, code utilizing DbContext still uses the same ObjectContext functionality underneath.

Besides that DbContext has some limitations compared to ObjectContext: for example, there's no out-of-the-box support for StoredProcedures using DbContext (except from stored procedures for CUD operations), so you would have to fallback to ObjectContext anyway.

Entity Framework 6 brings some benefits that are not visible in terms of code usage, such as - improved performance and bug-fixes. You will get those automatically using either of Context's.

EDIT

Personally, I'm all in for using DbContext as it is now a 'recommended' (in-fact, the only supported) way, but if you have a very large database model (with lots of tables, SPs, etc) and code base relying on it, migrating to DbContext would require you to make significant changes to your Data Layer that will not bring you benefits in a short-term. But it will definitely improve your system design and allow you to get future improvements in a long-term.

Entity Framework 7 announcement states significant changes upcoming (including new platforms and data stores support), which might or might not be useful in your case.

This effectively means you should carefully consider on what are your goals for now and for the future.

Upvotes: 1

Akash Kava
Akash Kava

Reputation: 39956

First point to consider, Microsoft is dropping ObjectContext support from EF7 onwards, so if you are planning to upgrade, you can do it now.

From normal coding perspective, DbContext or ObjectContext are not that dissimilar. Mostly everything is identical. DbContext has little longer startup time as it has to compile model but you can also put precompiled views to speed up.

DbContext wires up DataAnnotation Validations nicely, and it supports snapshot based change detection that actually improves performance compared to ObjectContext.

If you are using something heavy with reflection, then you will get problems because at runtime, objects are derived from runtime proxy instead of classes you declared. So it will need little bit of work. We had similar thing, but then we put an interface on each of our class to actually retrieve correct type for reflection.

DbContext will also support non database connections like Azure Table Storage or any of your own custom storage.

Mostly everywhere, DbContext is recommended. As it is going to stay. ObjectContext and its corresponding EntityObject are deprecated already.

Upvotes: 2

Related Questions