How to use ObjectContext.LoadProperty with EnablePlanCaching disabled?

804 views Asked by At

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.

1

There are 1 answers

0
AudioBubble On BEST ANSWER

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;
}