实体框架:如何禁用特定查询的延迟加载?

时间:2014-06-03 19:03:19

标签: c# entity-framework lazy-loading

有没有办法在Entity Framework 6上禁用延迟加载特定查询?我想定期使用它,但有时我想禁用它。我正在使用虚拟属性来延迟加载它们。

7 个答案:

答案 0 :(得分:62)

在要执行的查询之前设置以下代码

context.Configuration.LazyLoadingEnabled = false;

答案 1 :(得分:38)

您可以按如下方式禁用特定查询的延迟加载:

java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonSimple {
public static void main(String[] args) {

}

public HttpResponse http(String url, String body) {

    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
        HttpPost request = new HttpPost(url);
        StringEntity params = new StringEntity(body);
        request.addHeader("content-type", "application/json");
        request.setEntity(params);
        HttpResponse result = httpClient.execute(request);

        String json = EntityUtils.toString(result.getEntity(), "UTF-8");
        try {
            JSONParser parser = new JSONParser();
            Object resultObject = parser.parse(json);

            if (resultObject instanceof JSONArray) {
                JSONArray array=(JSONArray)resultObject;
                for (Object object : array) {
                    JSONObject obj =(JSONObject)object;
                    System.out.println(obj.get("example"));
                    System.out.println(obj.get("fr"));
                }

            }else if (resultObject instanceof JSONObject) {
                JSONObject obj =(JSONObject)resultObject;
                System.out.println(obj.get("example"));
                System.out.println(obj.get("fr"));
            }

        } catch (Exception e) {
            // TODO: handle exception
        }

    } catch (IOException ex) {
    }
    return null;
}}

答案 2 :(得分:17)

我可能在这里遗漏了一些东西,但是不是每次都改变配置,可能另一种方法是仅对那些你想要加载的查询使用.Include()吗?

假设我们有一个Product类,它具有Colour类的导航属性,您可以加载Colour这样的Product -

var product = _context.Products
    .Where(p => p.Name == "Thingy")
        .Include(x => x.Colours)
        .ToList();

答案 3 :(得分:15)

转到图表属性,找到指定为延迟加载的属性并禁用它。

如果您首先使用代码,请转到配置区域并使用以下命令禁用它:

this.Configuration.LazyLoadingEnabled = false;

答案 4 :(得分:4)

在EF Core中:context.ChangeTracker.LazyLoadingEnabled = false;

this answer

答案 5 :(得分:1)

假设您有这个:

IOrderedQueryable<Private.Database.DailyItem> items;
using (var context = new Private.Database.PrivateDb())
{
    context.Configuration.LazyLoadingEnabled = false;
    items = context.DailyItem.OrderBy(c => c.sortOrder).OrderByDescending(c => c.isFavorite);
}

尽管显式设置为to,您仍然会延迟加载。修复很容易,将其更改为此:

List<Private.Database.DailyItem> items;
using (var context = new Private.Database.PrivateDb())
{
    // context.Configuration.LazyLoadingEnabled = false;
    items = context.DailyItem.OrderBy(c => c.sortOrder).OrderByDescending(c => c.isFavorite).ToList();
}

答案 6 :(得分:1)

另一个版本的EF的另一种方式

context.ContextOptions.LazyLoadingEnabled = false;