1

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).
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
mare
  • 13,033
  • 24
  • 102
  • 191

2 Answers2

2

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.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
0

You are starting with the wrong assumption that you can't use lazy load with MVC.

If you manage the context at a higher level, you'll be able to do that without problems.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Check out the many examples that exist for session-per-request using NHibernate. A NH `ISession` roughly corresponds to an EF `DbContext` – Diego Mijelshon Mar 07 '11 at 14:09
  • I'm not using NHibernate but I do have IoC with Ninject in place and context per request, I guess that's it. However lazy loading in this configuration does not work with POCO context but it does with DbContext (just tried it out today). – mare Mar 07 '11 at 18:57