private static void IncludeReference <TEntity, TIncludingEntity, TIncludedEntity>(
     QueryContext queryContext,
     TEntity entity,
     TIncludedEntity relatedEntity,
     INavigation navigation,
     INavigation inverseNavigation,
     Action <TIncludingEntity, TIncludedEntity> fixup,
     bool trackingQuery)
     where TIncludingEntity : TEntity
 {
     if (entity is TIncludingEntity includingEntity)
     {
         if (trackingQuery &&
             navigation.DeclaringEntityType.FindPrimaryKey() != null)
         {
             // For non-null relatedEntity StateManager will set the flag
             if (relatedEntity == null)
             {
                 queryContext.SetNavigationIsLoaded(includingEntity, navigation);
             }
         }
         else
         {
             SetIsLoadedNoTracking(includingEntity, navigation);
             if (relatedEntity != null)
             {
                 fixup(includingEntity, relatedEntity);
                 if (inverseNavigation != null &&
                     !inverseNavigation.IsCollection())
                 {
                     SetIsLoadedNoTracking(relatedEntity, inverseNavigation);
                 }
             }
         }
     }
 }
            private static void InitializeIncludeCollection <TParent, TNavigationEntity>(
                int collectionId,
                QueryContext queryContext,
                DbDataReader dbDataReader,
                ResultCoordinator resultCoordinator,
                TParent entity,
                Func <QueryContext, DbDataReader, object[]> parentIdentifier,
                Func <QueryContext, DbDataReader, object[]> outerIdentifier,
                INavigation navigation,
                IClrCollectionAccessor clrCollectionAccessor,
                bool trackingQuery)
                where TNavigationEntity : TParent
            {
                object collection = null;

                if (entity is TNavigationEntity)
                {
                    if (trackingQuery)
                    {
                        queryContext.SetNavigationIsLoaded(entity, navigation);
                    }
                    else
                    {
                        SetIsLoadedNoTracking(entity, navigation);
                    }

                    collection = clrCollectionAccessor.GetOrCreate(entity, forMaterialization: true);
                }

                var parentKey = parentIdentifier(queryContext, dbDataReader);
                var outerKey  = outerIdentifier(queryContext, dbDataReader);

                var collectionMaterializationContext = new CollectionMaterializationContext(entity, collection, parentKey, outerKey);

                resultCoordinator.SetCollectionMaterializationContext(collectionId, collectionMaterializationContext);
            }