Entity Framework - Include / Reference / Collection

The only purpose of the Include() method is to explicitly eager load related data upon querying.

The Entry() method - on the other hand - is intended to give you specific control over the current state of an Entity attached to the context and not only Load() related data.

That is the reason why you have to explicitly choose between Collection, Reference and Property methods, each one exposes different set of functionality (hence returns different type).

For example:

  • Scalar (DbPropertyEntry) contains the IsModified property that denotes whether the value changed from 'x' to 'y' (for example).

  • Reference (DbReferenceEntry) contains the IsLoaded property that denotes whether the referenced data has been loaded from the database already.

  • Reference Collection (DbCollectionEntry) derives from ICollection (hence IEnumerable as well) which means that you can iterate over its data. Yet, it cannot contain an IsModified property because it may differ for each item in the collection.

Still, if you're only interested in Load(), you can leverage the polymorphic Member() method (that returns DbMemberEntry that is the base type to all of the above types) and check if the entry is "Loadable":

var memberEntry = this.Entry(entity).Member("NavigationProperty");

if (memberEntry is DbCollectionEntry collectionMember)
    collectionMember.Load();

if (memberEntry is DbReferenceEntry referenceMember)
    referenceMember.Load();

You can do it this way:

1.- Load the entity including the collections:

MyClass myObject = dbContext.MyClasses
                    .Include(cls => cls.ObjectCollection)
                    .Single(cls => cls.Pk == entityPk);

2.- Then you must retrieve that object Entry and tell EF to load the required properties in the collection objects:

dbContext.Entry(myObject).Collection("ObjectCollection").Query().Include("ReferenceClass").Load(); 

Further reading:

http://msdn.microsoft.com/en-us/data/jj574232#explicitFilter