Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoadProperty in Entity Framework 5

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.

like image 357
citronas Avatar asked Oct 07 '12 18:10

citronas


People also ask

How do I enable lazy loading in Entity Framework?

All the developer has to do is install Microsoft. EntityFrameworkCore. Proxies package which will add all the required proxies needed to run Lazy Loading. After installing the package, the system will ask the developer to allow the installed proxies to access the databases and enable lazy loading.

What is explicit loading in EF?

3) Explicit Loading : This type of loading if force loading. This behaviour is like lazy loading but lazy loading disabled (in EF 6), it is still possible to lazily load related entities, but it must be done with an explicit call. We can implement it with use the Load() method to load related entities explicitly.

What is the way of loading data in Entity Framework?

Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed.

What is eager loading in EF core?

Eager loading means that the related data is loaded from the database as part of the initial query. Explicit loading means that the related data is explicitly loaded from the database at a later time.


1 Answers

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

like image 166
chris vietor Avatar answered Sep 20 '22 19:09

chris vietor