开关与惰性加载存储库模式 [英] Switches for LazyLoading with Repository pattern

查看:131
本文介绍了开关与惰性加载存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在默认情况下惰性加载在我的DbContext被禁用。我使用存储库模式,在某些情况下,我需要得到只有简单的对象和其他我需要的对象与导航属性的值。

By default LazyLoading is disabled in my DbContext. I use repository pattern and in some case I need to get only simple object and in other I need to get object with values of navigations properties.

我如何能实现类似的惰性加载交换机?
结果任何帮助将AP preciated

How can I implement something like switches for LazyLoading?
Any help will be appreciated

我有一个解决方案,它的作品,但我不知道这是正确的:
在仓库的界面我添加新的属性。

I have one solution that works but I'm not sure that it is correct: in interface of repository I added new property

    public interface IRepository<T> where T : BaseEntity
    {
        T GetById(object id);
        void Insert(T entity);
        .....
        bool LazyLoadingSwitches { set; }
    }

然后实现它:

public bool LazyLoadingSwitches 
{
    set
    {
        this.context.Configuration.LazyLoadingEnabled = value;
    } 
}

,当我需要与相关数据模型,然后在我使用的控制器:

and when I need to get model with related data then I use in controller:

repository.LazyLoadingSwitches = true;
name = order.Customer.FullName; 
repository.LazyLoadingSwitches = false;

请建议我什么是最好的解决办法?

Please suggest me what is the best solution for that?

推荐答案

我认为你可以使用包括:

I think you can use include:

order.Include("Customer");
var name = order.Customer.FullName;

样品与拉姆达前pression:

Sample with lambda expression:

order.Include(o => o.Customer);
var name = order.Customer.FullName;

这篇关于开关与惰性加载存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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