How do I get a related Entity to Load other than the initial .Include() calls - can I do it just using an Entity alone? RRS feed

  • Question

  • User-1851576699 posted

    If I have an Entity with multiple relationships, lets say 5, in total. But initially I only want to load 3 of those relationships, so I use the .Include() method of a DbQuery<> which gets me a single entity. 

    So, I didn't choose to fully populate my entity, however, Entity Framework appears to always populate the related(foreign keys) of an Entity regardless of whether or not you chose to populate the related entities all at the time of instantiation(isn't this a word Microsoft? Please put it on the whitelist, and put the word "whitelist" on the whitelist at the same time). 

    So my question is, simply, can I trigger the instantiation of related entities at a later time without performing a new query on the database without doing a search for the entity myself?

    So, I know of 2 different ways to do this already but it seems like there should be a 3rd way. Just by saying, "Hey, I already got the key, just instantiate the relationship for me now".

    Edit: LazyLoadingEnabled = true on my context.

    Edit: Okay, I figured this out. I remember from this from awhile back. You can't trigger a related Entity load from a single Entity. It looks like you have to trigger the entire related Entity set by enumerating it. Then the related entities will be available.

    Thursday, September 17, 2015 9:34 PM