我有以下关系
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
}
现在,我使用
加载SharedResourceSharedResource sharedResource = Get(shareKey)
并且
sharedResource.PackageVersion == null.
虽然VersionId不为null且
context.Configuration.LazyLoadingEnabled = false;
为了加载它我该怎么做
答案 0 :(得分:6)
LazyLoadingEnabled
必须是true
,而不是false
:
context.Configuration.LazyLoadingEnabled = true;
如果您根本没有设置true
,则 LazyLoadingEnabled
是默认设置。
PackageVersion
属性必须为virtual
才能为此属性启用延迟加载。
或者您可以直接在查询中包含该属性:
SharedResource sharedResource = context.SharedResource
.Include("PackageVersion")
.SingleOrDefault(s => s.Id == shareKey);