我有这个类;
[Table("tblRegions")]
public class Region : MasterEntity
{
public string Code { get; set; }
public string Description { get; set; }
public Region ParentRegion { get; set; }
public Country Country { get; set; }
public RegionType RegionType { get; set; }
}
结果是Region, Country RegionType字段被创建为DB中的外键字段,并且ID值被正确保存。
问题是在检索区域,parentreregion, Country和RegionType是空的,但在数据库中我看到他们的Id值。
您忘记将它们标记为虚拟了。
public virtual Region ParentRegion { get; set; }
...
这就是如何让EF在自动生成的延迟加载父实体的代理类中创建被覆盖的属性。