我有以下关系



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;


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

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

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

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

07-24 09:26