When are navigational properties loaded?
If an entity A has a FK relationship with entity B, and is represented as a n开发者_JAVA技巧avigational property in entity A inside EF4, when are data from entity B loaded? Is it upon creating an instance of A, or only when B is accessed from within A?
It depends on loading method:
- Eager loading - the query loading A contains
.Include(a => a.B)
. In such case both A and related Bs are loaded during query execution - Lazy loading - only A is loaded during the first query and if it is still in scope of living context it can trigger lazy loading of B once navigation property accessed first time
- Explicit loading - you will manually trigger loading by calling
context.LoadProperty(a, "B");
You're going to want to look into Loading Related Objects, perhaps you're more interested in eager loading.
精彩评论