Search code examples
entity-framework-4asp.net-mvc-3pocoentity-framework-ctp5ef-code-first

EF CTP5 which code generation item to choose?


With EF 4.1 on the way and CTP5 being available for few months now I've decided to try out the new functionality. As I see, there are multiple generation items available (DbContext and three different ObjectContext's). I also noticed they are not interchangable - I was first using the POCO ObjectContext in one of my app and today switched to DbContext and my entire repository broke. It was based on LoadProperty() methods, DeleteObject() and AddObject() methods and those are all missing on DbSet class which is used in DbContext generation.

I know there's a great blog series here http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx introducing the new functionality but it never really says when to choose what.

My requirements are:

  • ASP.NET MVC application, so lazy loading mostly won't work coz at page render it will say that context has already been disposed (that's why I need easy support for explicit loading - in EF4 I did it via Include(), using POCO context I did it through LoadProperty() and now in DbContext I believe I will use the strongly typed Include()).
  • We probably won't be needing code-first features (but you never know).

Solution

  • Difference between those two is mostly in API and feature set. DbContext of course have Include for query and Load but you will find it elsewhere. Moreover when using CTP5 assembly you will have strongy typed Include for both ObjectSet and DbSet (available on IQueryable interface as extension method).

    Explicit loading (equivalent to LoadProperty) is performed by Load method on DbReferenceEntry<T> or DbCollectionEntry<T> - check Explicit loading related entities. It works even better then LoadProperty because you can define filter for loading.