EF Core 返回空关系直到直接访问

编程入门 行业动态 更新时间:2024-10-22 11:34:10
本文介绍了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; } }

在 DBContext 的 OnModelCreating 中,我建立了如下关系:

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 返回空关系直到直接访问

本文发布于:2023-11-16 18:35:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   EF   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!