public void LoadEntities()
        {
            if (!UnloadedIds.Any())
            {
                return;
            }

            var dbEntityReader = new EntityReader <TEntity>(Connection);
            var entities       = dbEntityReader.AsQueryable().WherePropertyMatches(ForeignProperty, UnloadedIds);

            foreach (var entity in entities)
            {
                Update(entity, EntityEntryState.NoChanges);
            }

            UnloadedIds.Clear();
        }
        public void LoadEntities()
        {
            if (!UnloadedIds.Any())
            {
                return;
            }

            var dbEntityReader = new DbEntityReader <TEntity>(Database, EntityMapper);
            var entities       = dbEntityReader.AsQueryable().WherePropertyMatches(ForeignKey, ForeignPropertyMap.PropertyType, UnloadedIds);

            foreach (var entity in entities)
            {
                Update(entity, DbEntityEntryState.NoChanges);
            }

            UnloadedIds.Clear();
        }
        public void AddForeignId(object foreignId)
        {
            if (foreignId == null)
            {
                throw new ArgumentNullException(nameof(foreignId));
            }

            //Check the EntityId matches the known type for TEntity
            if (!ForeignProperty.PropertyType.Equals(foreignId.GetType()))
            {
                throw new InvalidOperationException($"Type mismatch for foreign key. {foreignId.GetType()} specified however expected type {ForeignProperty.PropertyType}");
            }

            //Check the entity isn't already loaded
            if (!Entries.Any(e => Equals(foreignId, ForeignProperty.GetValue(e.Entity))))
            {
                UnloadedIds.Add(foreignId);
            }
        }
        public new IEnumerator <TEntity> GetEnumerator()
        {
            //Enumerate loaded entities
            var result = Entries.Select(e => e.Entity);

            using (var enumerator = result.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    yield return(enumerator.Current);
                }
            }

            //Enumerate list of unloaded IDs and load them in one at a time
            if (UnloadedIds.Any())
            {
                var dbEntityReader   = new EntityReader <TEntity>(Connection);
                var unloadedEntities = dbEntityReader.AsQueryable().WherePropertyMatches(ForeignProperty, UnloadedIds);

                using (var unloadedEnumerator = unloadedEntities.GetEnumerator())
                {
                    while (unloadedEnumerator.MoveNext())
                    {
                        var loadedEntity = unloadedEnumerator.Current;

                        //Load the data into the collection so we don't need to query it again
                        Update(loadedEntity, EntityEntryState.NoChanges);

                        //Remove from unloaded entity collection
                        var foreignId = ForeignProperty.GetValue(loadedEntity);
                        UnloadedIds.Remove(foreignId);

                        yield return(loadedEntity);
                    }
                }
            }
        }