EF Core 返回空关系直到直接访问 [英] EF Core returns null relations until direct access

查看:20
本文介绍了EF Core 返回空关系直到直接访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像下面这样的模型:

I have some models like those below:

public class Mutant
{
    public long Id { get; set; }
    ...

    // Relations
    public long OriginalCodeId { get; set; }
    public virtual OriginalCode OriginalCode { get; set; }
    public int DifficultyLevelId { get; set; }
    public virtual DifficultyLevel DifficultyLevel { get; set; }
}

public class OriginalCode
{
    public long Id { get; set; }
    ...

    // Relations
    public virtual List<Mutant> Mutants { get; set; }
    public virtual List<OriginalCodeInputParameter> OriginalCodeInputParameters { get; set; }
}

DBContextOnModelCreating 中,我建立了如下关系:

and in the OnModelCreating of DBContext I made the relations like these:

        modelBuilder.Entity<Mutant>()
            .HasOne(m => m.OriginalCode)
            .WithMany(oc => oc.Mutants)
            .HasForeignKey(m => m.OriginalCodeId)
            .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);

        modelBuilder.Entity<Mutant>()
            .HasOne(m => m.DifficultyLevel)
            .WithMany(dl => dl.Mutants)
            .HasForeignKey(m => m.DifficultyLevelId)
            .OnDelete(Microsoft.EntityFrameworkCore.Metadata.DeleteBehavior.Restrict);

现在当我请求 Mutants 时,OriginalCode 为空:

now when I request for Mutants, the OriginalCode is null:

但是一旦我请求 OriginalCode 如下所示:

but as soon as I request for OriginalCodes like below:

那么突变体的OriginalCode字段将不为空:

then the OriginalCode field of the mutants will be not null:

原因是什么,我该如何解决?

What is the reason and how could I fix it?

推荐答案

原因在加载相关数据 EF Core 文档部分.

The reason is explained in the Loading Related Data section of the EF Core documentation.

第一个行为是因为 EF Core 当前不支持延迟加载,因此通常您会获得 null 导航属性,直到您通过急切或显式加载专门加载它们.但是,Eager loading 部分包含以下内容:

The first behavior is because EF Core currently does not support lazy loading, so normally you'll get null for navigation properties until you specifically load them via eager or explicit loading. However, the Eager loading section contains the following:

提示
Entity Framework Core 将自动修复以前加载到上下文实例中的任何其他实体的导航属性.因此,即使您没有明确包含导航属性的数据,如果之前加载了部分或全部相关实体,该属性仍可能会被填充.

Tip
Entity Framework Core will automatically fix-up navigation properties to any other entities that were previously loaded into the context instance. So even if you don't explicitly include the data for a navigation property, the property may still be populated if some or all of the related entities were previously loaded.

这就解释了为什么导航属性在第二种情况下不为空.

which explains why the navigation property is not null in the second case.

现在,我不确定您要修复这两种行为中的哪一种,因此将尝试解决这两个问题.

Now, I'm not sure which of the two behaviors do you want to fix, so will try to address both.

第一个行为可以通过使用当前可用的加载相关数据的方法之一来修复",例如预先加载:

The first behavior can be "fixed" by using one of the currently available methods for loading related data, for instance eager loading:

var mutants = db.Mutants.Include(m => m.OriginalCode).ToList();

第二种行为是设计使然",无法控制.如果您想避免它,请确保使用全新的 DbContext 实例仅用于执行单个查询以重试所需的数据.

The second behavior is "by design" and cannot be controlled. If you want to avoid it, make sure to use fresh new DbContext instance just for executing a single query to retry the data needed.

更新:从 v2.1 开始,EF Core 支持 延迟加载.但是默认情况下它没有启用,因此为了使用它,应该将所有导航属性标记为 virtual,安装 Microsoft.EntityFrameworkCore.Proxies 并通过 UseLazyLoadingProxies 调用启用它,或使用 Lazy-loading without proxies - 两者都在 EF Core 文档中通过示例进行了解释.

Update: Starting with v2.1, EF Core supports Lazy Loading. However it's not enabled by default, so in order to utilize it one should mark all navigation properties virtual, install Microsoft.EntityFrameworkCore.Proxies and enable it via UseLazyLoadingProxies call, or utilize Lazy-loading without proxies - both explained with examples in the EF Core documentation.

这篇关于EF Core 返回空关系直到直接访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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