Wednesday, May 15, 2013

Entity Framework: Explicit Loading

One of the features that Entity Framework offers is Explicit Loading. This allows you to lazily load related entities, even with lazy loading disabled. The only difference is that you need a explicit call to do it(by using the Load method on the related entity’s entry):

But what if you want to filter the child results you get back. For example, imagine that you only want to know the female students in your class(no idea why you would want to know this Glimlach

Let’s try to change our code to do this:

However if we look at the results we get back, it still contains all students. To enable filtering when using explicit loading, you’ll have to make sure that LazyLoading is disabled. If we change the code to incorporate this, it will work as expected:

No comments: