实体框架6的`ObjectQuery.EnablePlanCaching`-它在哪里? [英] Entity Framework's 6's `ObjectQuery.EnablePlanCaching` - where is it?

查看:95
本文介绍了实体框架6的`ObjectQuery.EnablePlanCaching`-它在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本文解释说可以通过设置来停用查询计划缓存ObjectQuery上的EnablePlanCachingfalse,并且默认情况下已在EF6中将其启用.

This article explains that query plan caching can be deactivated by setting EnablePlanCaching to false on the ObjectQuery, and also that it is enabled by default in EF6.

A,EF6没有ObjectQueryDbSet为您提供DbQuery.

Alas, EF6 doesn't have an ObjectQuery, the DbSet's give you DbQuerys.

我认为我不能从DbQuery中获得ObjectQuery,我当然也不想使用旧的ObjectContext.是这样还是有办法在EF6中禁用计划缓存?

I don't think I can get an ObjectQuery from the DbQuery, and I certainly don't want to use the old ObjectContext. So is that it or is there a way to disable plan caching in EF6?

推荐答案

此处是扩展方法,可让您从DbSet中获取ObjectQuery

Here is an extension method that will let you get an ObjectQuery from the DbSet

using (var ctx = new TestContext())
{
    var query = ctx.Products;
    query.GetObjectQuery().EnablePlanCaching = false;
    var list = query.ToList();
}


namespace Z.EntityFramework.Plus
{
    internal static partial class InternalExtensions
    {
        /// <summary>An IQueryable&lt;TEntity&gt; extension method that get the ObjectQuery from the query.</summary>
        /// <typeparam name="T">The type of elements of the query.</typeparam>
        /// <param name="query">The query to get the ObjectQuery from.</param>
        /// <returns>The ObjectQuery from the query.</returns>
        internal static ObjectQuery<T> GetObjectQuery<T>(this IQueryable<T> query)
        {
            // CHECK for ObjectQuery
            var objectQuery = query as ObjectQuery<T>;
            if (objectQuery != null)
            {
                return objectQuery;
            }

            // CHECK for DbQuery
            var dbQuery = query as DbQuery<T>;

            if (dbQuery == null)
            {
                throw new Exception("Oops! A general error has occurred. Please report the issue including the stack trace to our support team: info@zzzprojects.com");
            }

            var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance);
            var internalQuery = internalQueryProperty.GetValue(dbQuery, null);
            var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance);
            var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null);

            objectQuery = objectQueryContext as ObjectQuery<T>;

            return objectQuery;
        }
    }
}

这篇关于实体框架6的`ObjectQuery.EnablePlanCaching`-它在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆