Question

On an ObjectContext object, I'm using code like this to load navigation properties.

    context.LoadProperty(entity, navigationProperty, 
        System.Data.Objects.MergeOption.AppendOnly);

I would like to disable plan caching on queries that will be generated with this kind of call ?

Is it possible ?

Is there an alternative by wrapping the context with a DBContext ?

Thanks in advance.

Was it helpful?

Solution

I solved the problem by using a DBContext.

DbContext dbc = new DbContext(context, false);
dbc.Entry<T>(object).Reference<TReference>(@"ReferenceName").Query().DisablePlanCaching().Load();
dbc.Entry<T>(object).Collection<TCollection>(@"CollectionName").Query().DisablePlanCaching().Load();

with DisablePlanCaching extension method (inspired from the one found here) as :

public static IQueryable<T> DisablePlanCaching<T>(this IQueryable<T> query)
{
    ObjectQuery<T> q = query as ObjectQuery<T>;
    if ( q == null )
        throw new InvalidOperationException(@"IQueryable<T> is not of type ObjectQuery<T>");

    q.EnablePlanCaching = false;
    return query;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top