Context.CreateQuery & lt; T & gt; is loading everything?


I have been working away for EF4 and got a lot of stuff done and its working really nicely, the one problem I do have is: Context.CreateQuery returns correctly, but it is also loading ALL its related entities too!?

Which is going to cause massive issues once the DB actually has real data in there.

Any ideas on how to stop it from loading all the related entities?

Here is snippet of what I have:

The original call:

public ObjectNameHere GetById(Guid id)
        return Query(p => p.Id == id).SingleOrDefault();

The Provider Base

private static Type GetBaseType(Type type)
        var baseType = type.BaseType;
        if (baseType != null && baseType != typeof(EntityObject))
            return GetBaseType(type.BaseType);
        return type;

    private static bool HasBaseType(Type type, out Type baseType)
        var originalType = type.GetType();
        baseType = GetBaseType(type);
        return baseType != originalType;

    private IQueryable<T> CreateQuery<T>()
        Type baseType;
        return HasBaseType(typeof(T), out baseType)
                   ? Context.CreateQuery<T>(ProviderHelper.GetEntitySetName(Context, baseType.Name)).OfType<T>()
                   : Context.CreateQuery<T>(ProviderHelper.GetEntitySetName(Context, baseType.Name));

The Provider Helper

    public static string GetChildTypeNames(Type[] childTypes)
        var context = ContextProvider.Context;

        var childNames = new StringBuilder();

        for (var i = 0; i < childTypes.Count(); i++)

            if (i != childTypes.Count()) { childNames.Append("."); }
        return childNames.ToString();

    public static string GetEntitySetName(ObjectContext context, string entityTypeName)

        var container = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace);
        return (from meta in container.BaseEntitySets
                                where meta.ElementType.Name == entityTypeName
                                select meta.Name).FirstOrDefault();

have a look here: