Is there a way to automatically load a navigation property using the .NET Entity Framework?

Moving further and further away from writing SQL for my applications, I decided to try Entity Framework. However, I ran into something that I think forces me to write more code than I think is strictly necessary.

When I accessed some of the navigation properties, I found that all one-to-one relationships (simple links) were empty, and all one-to-many and many-to-many relationships (EntityCollections) were empty.

For example: I have a User with a link to a group. When I returned the user using a simple select-by-id, the Group property is null. If I want to access a group, I have to manually load it (using User.GroupReference.Load ()). So I added a GetGroup () method to User that checks if the group is already loaded and if not, does it and then returns the group.

This will now lead to many very similar methods for all navigation properties. And all this leads to the fact that the navigation properties are not used, now only my Get "PropertyName" () method is used.

I don't want to extend my queries (linq for entities) to load all these properties at once, because at first it is not always known what is needed. And besides, it will generate a lot of requests.

Is there a way to configure Entity Framework to load these entities when they are not present? So when I access User.Group and the group is not loaded yet, is it loaded automatically? Or am I sticking with using my own "PropertyName" () Get method while I'm trying to load objects only on demand (or just in time)?

Additional info: I am using VS2008 SP1 with .NET 3.5 SP1. I am using the Entity Framework that shipped with it.

+2


a source to share


1 answer


You can get EFLazyLoading

- this will add transparent lazy loading support to Entity Framework by replacing the code generator.



There is no built-in support for transparent lazy loading in Entity Framework 1.0 - only for loading using Include()

and manual lazy loading using Load()

. I'm not sure about Entity Framework 2.0, but if there is no built-in support, at least it's easier to add it than before due to the use of T4 for code generation.

+1


a source







All Articles