导航属性不加载 [英] Navigation properties not loading propertly

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

问题描述

我的上下文看起来像:

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

    //DbSet properties
}

所以,懒惰加载启用。

我有以下类:

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 关键字。 至今如我所知,当我们不使用 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; - preperties not loaded - 它们仍然是 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.

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

$ b

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?

推荐答案

p>如果不使用 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 .

这并不意味着您将立即填写实体的所有属性,以填充幻灯片在您的代码中,您必须使用.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 .

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

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 Eager加载所有导航属性

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

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