5

我有以下关系

在此处输入图像描述

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;

我必须做什么才能加载它

4

1 回答 1

6

LazyLoadingEnabled必须true,不是false

context.Configuration.LazyLoadingEnabled = true;

true如果您根本不设置,则为默认值LazyLoadingEnabled

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

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

SharedResource sharedResource = context.SharedResource
    .Include("PackageVersion")
    .SingleOrDefault(s => s.Id == shareKey);
于 2013-04-15T18:49:15.733 回答