gpt4 book ai didi

entity-framework - Entity Framework - 导航属性未加载

转载 作者:行者123 更新时间:2023-12-02 06:17:44 25 4
gpt4 key购买 nike

我有以下关系

enter image description here

public partial class SharedResource : DomainEntity
{
public System.Guid Id { get; set; }
public System.Guid VersionId { get; set; }

public virtual PackageVersion PackageVersion { get; set; } // tried it noth with and without virtual
}

现在,我使用

加载 SharedResource
SharedResource sharedResource = Get(shareKey)

sharedResource.PackageVersion == null. 

尽管 VersionId 不为 null 并且

context.Configuration.LazyLoadingEnabled = false;

我需要做什么才能加载它

最佳答案

LazyLoadingEnabled 必须为 true,而不是 false:

context.Configuration.LazyLoadingEnabled = true;
如果您根本没有设置 LazyLoadingEnabled,则

true 是默认值。

并且 PackageVersion 属性必须是 virtual 才能启用此属性的延迟加载。

或者您可以直接在查询中包含该属性:

SharedResource sharedResource = context.SharedResource
.Include("PackageVersion")
.SingleOrDefault(s => s.Id == shareKey);

关于entity-framework - Entity Framework - 导航属性未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com