我首先使用 Entity Framework 4.1 代码,我有两个实体和一个抽象类,这两个实体都继承自。
public abstract class Customer
{
public long CustomerId { get; set; }
public string Rating { get; set; }
public int FinancialStatusValue { get; internal set; }
}
public class Organization : Customer
{
public string Name { get; set; }
public string Name2 { get; set; }
public string LegalName { get; set; }
public string OrganizationNumber { get; set; }
public string Vat { get; set; }
public string Duns { get; set; }
public Organization HeadQuarter { get; set; }
public virtual ICollection<Organization> ChildOrganizations { get; set; }
}
我将我的模型映射为:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Customer>().ToTable("Customer");
modelBuilder.Entity<Organization>().ToTable("Organization");
modelBuilder.Entity<Organization>().HasOptional(h => h.HeadQuarter)
.WithMany(c => c.ChildOrganizations)
.HasForeignKey(o => o.ParentId);
}
然后我使用以下方式查询组织:
var org = ctx.Customers.OfType<Organization>().Single(c => c.CustomerId == 259033);
ParentId 已填充,但 HeadQuarter 和 ChildOrganizations 始终为空。
我错过了什么?