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

查看:146
本文介绍了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; }
}

OnModelCreating DBContext 的c>我做了如下关系:

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为null:

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

,但我一经要求输入 OriginalCode s如下所示:

but as soon as I request for OriginalCodes like below:

然后将 OriginalCode 字段将不为空:

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

What is the reason and how could I fix it?

推荐答案

原因在< E的href = https://docs.microsoft.com/en-us/ef/core/querying/related-data rel = noreferrer>加载相关数据部分F Core文档。

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

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

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 调用启用它,或使用没有代理的延迟加载-两者用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天全站免登陆