导航属性未正确加载

Navigation properties not loading properly(导航属性未正确加载)

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

问题描述

我的上下文如下所示:

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; }
}

请注意,导航属性 HeaderSlides 都使用 没有 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 未加载 - 他们仍然 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 获取数据的方式(使用存储库模式):

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

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

本文标题为:导航属性未正确加载

基础教程推荐