Abp 框架 - 不包括外键关系

时间:2021-06-04 18:12:40

标签: c# entity-framework .net-core entity-framework-core abp

我有以下数据模型:

Class Person  : Entity<long>
{
    // Id is ABP primary key
    public string name {get;set;}
}

Class Car  : Entity<long>
{
    // Id is ABP primary key
    public long PERSONID { get; set; }
    [ForeignKey("PERSONID")]
    public Person PersonModel { get; set; }
}

在 dbContext 中:

modelBuilder.Entity<Person>().Property(i => i.Id).HasColumnName("IDPERSON");
modelBuilder.Entity<Car>().HasIndex(p => new { p.PERSONID, p.OTHERKEY }).IsUnique();

结果见下表:

餐桌人

IDPERSON,NAME

桌车

Id, PERSONID, OTHERKEY

问题:当我使用 CarGet 从存储库 GetAll 检索记录时,它不包括模型 Person

{
    Id: 1,
    PERSONID: 1,
    Person: null
}

注意:表中存在带键的记录

有人知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

你必须使用Include

var car= dbContext.Cars.Include(p=> p.PersonModel).FirstOrDefault(c=>c.Id==carId);

或得到所有

var cars= dbContext.Cars.Include(p=> p.PersonModel).ToList();
相关问题