导航属性无法正确加载 [英] Navigation properties not loading properly

查看:143
本文介绍了导航属性无法正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的上下文如下:

public class ApplicationDbContext: IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
        this.Configuration.LazyLoadingEnabled = true;
    }

    //DbSet properties
}

因此,启用了延迟加载.

so, lazy loading is enabled.

我有以下课程:

public class Home
{
    private ICollection<Slide> _slides;

    [Key]
    [Required]
    public string Name { get; set; }

    [ForeignKey("Header")]
    public int? HeaderID { get; set; }

    //Navigation properties
    public ICollection<Slide> Slides
    {
        get { return _slides ?? (_slides = new List<Slide>()); }
        set { _slides = value; }
    }

    public Content Header { get; set; }
}

请注意,没有 virtual关键字都使用了导航属性HeaderSlides. 到目前为止据我所知,当我们不使用virtual关键字时-属性应尽快加载.

Note that both navigation properties Header and Slides are used without virtual keyword. As far as I know when we don't use virtual keyword - properties should load eagerly.

但是,当我从数据库中获取我的Home实体时,两个导航属性都是null(但是属性HeaderID具有值).
即使我切换到this.Configuration.LazyLoadingEnabled = false;-未加载属性-它们仍然null.

However, when I get my Home entity from database, both my navigation properties are null (but property HeaderID has value).
Even if I switch to this.Configuration.LazyLoadingEnabled = false; - preperties not loaded - they still null.

这是我从数据库(使用存储库模式)获取数据的方式:

Here is how I get my data from db (using repository pattern):

public static Home GetHomeComponent(
    this IRepositoryAsync<Home> repository)
{
   var result = repository
       .Query()
       .Select()
       .First();
   return result;
}

我通过Include属性解决了我的问题:

I solved my problem with Include properties:

public static Home GetHomeComponent(
    this IRepositoryAsync<Home> repository)
{
   var result = repository
       .Query()
       .Include(x => x.Header)
       .Include(x=>x.Slides)
       .Select()
       .First();
   return result;
}

但是,这对我来说并不方便(因为我要加载的导航属性过多).

However it's not convenient for me (since I have too much navigation properties to load).

所以,我的问题是:
使用virtual关键字-但是为什么我的导航属性没有急切加载?
还是我做错了什么?有没有其他方法可以在不使用Include的情况下加载我的导航属性?

So, my question is:
I don't use virtual keyword - but why my navigation properties not loading eagerly?
Or I'm doing something wrong? Is there any other way to load my navigation properties without using Include?

推荐答案

如果您不使用virtual关键字,则仅表示一旦尝试访问非虚拟属性,它将不会从数据库中加载,但是您会得到Null.

If you don't use the virtual keyword it only means that once you try to access the non-virtual property it wont be loaded from the database but you will get a Null .

这并不意味着您将立即拥有实体的所有属性,要在代码中为EG填充Slides,您必须使用.include()-这是急于加载,加载属性在使用之前由您自己决定.

It doesn't mean you will have all the properties of the entities populated right away, to populate Slides for E.G in your code, you have to use .Include() - this is eager loading, to load the property by your self before it used .

您可以创建一个通用函数,该函数将通过其获取的参数填充所需的属性(使用params),有关更多详细信息,请参见此处:

You can make a generic function that will populate the required properties by the arguments it gets ( using params ) see here for more details :

EntityFramework渴望加载所有导航属性

这篇关于导航属性无法正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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