Search code examples
c#entity-framework-corelazy-loadingnavigation-properties

Entity framework core, How load navigational parameters only if its not loaded before?


Once Lazy Loading is not yet implemented on Entity Framework Core, How can I load a navigational property from a class, but only if it was not loaded before using Entity Framework Core. For instance. this class

class MyClass{
    // ...
    IEnumerable<Child> Children {get;set;}

    public int CountChildren(){
         return children.Count();
    }
}

I will only able to count the children using myinstance.CountChildren() if Children was loaded before, for instance using eager loading or explicit loading.

But I would like to verify if the Children navigational property was loaded before. If not, then force it to load. Is this somehow possible? how can I do it?


Solution

  • After some tests I was able to know that when you use load() method, it only will load from database when it was not loaded before.

    In this case you could always load properties on demand, if it was already loaded before it will not be loaded again.