实体框架:如何禁用特定查询的延迟加载? [英] Entity Framework: How to disable lazy loading for specific query?

查看:104
本文介绍了实体框架:如何禁用特定查询的延迟加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any way to disable lazy loading for specific query on Entity Framework 6? I want to use it regularly, but sometimes I want to disable it. I'm using virtual properties to lazy load them.

推荐答案

您可以禁用Lazy加载特定查询,如下所示:

You can disable Lazy loading for specific query as follows :

public static Cursos GetDatosCursoById(int cursoId)
{
    using (var bd = new AcademyEntities())
    {
        try
        {
            bd.Configuration.ProxyCreationEnabled = false;
            return bd.Cursos.FirstOrDefault(c => c.cursoId == cursoId);
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}

这篇关于实体框架:如何禁用特定查询的延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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