7

I upgraded my entity framework 4.3 database first project to the new entity framework 5. Apparently I'm now using DbContext instead of ObjectContext.

I've replaced my old .edmx file with a new one. My old business code, that was previously using my 4.3 .edmx file, now has a problem with code using the LoadProperty method:

using (var context = new MyEntities())
{
    Models.User user = context.Users.First(x => x.GUID == guid);
    context.LoadProperty(user, o => o.Settings);
    return user;
}

It seems that LoadProperty is not an available method in DbContext.

How can I can get strong typed loading anyway?

I suppose I could use

context.Users.Include("Settings")

but that is not strong typed and prone to typos.

citronas
  • 19,035
  • 27
  • 96
  • 164
  • 1
    You were not using EF4.3 if you used ObjectContext - EF4.3 was basically all about DbContext... If lazy loading is enabled you can just use .Settings property without having to explicitly load it. Note that it may trigger a trip to a database so if you are doing this frequently it may be cheaper to use .Include() as pointed below. – Pawel Oct 08 '12 at 03:13
  • @Pawel: Oh sorry. I read a question about another problem I encountered after the update and quoted it with "apprently" to better describe my situation. I striked that sentence through. – citronas Oct 08 '12 at 07:21

1 Answers1

15

You can use the Include method with Lambda too. don't forget the using statement, because this Include comes from the DbExtension class:

using System.Data.Entity;

...

context.Users.Include(u => u.Settings);

here is some info about the Include extension method: msdn info

chris vietor
  • 2,050
  • 1
  • 20
  • 29