在2个表中包括第三个表,它们之间具有2个关系

时间:2019-02-08 14:58:01

标签: c# entity-framework model-view-controller asp.net-core razor-pages

在我的数据模型中,表BusinessGlosItem和表Reference之间有2种关系-当我在2个BusinessGlosItems之间创建引用时,我想看看它是什么类型的引用,所以这就是为什么我需要包含ReferenceType的原因。

我需要帮助的是如何将ReferenceType包含到我的IndexModel中,这样我就可以创建一个剃刀html表,以查看其中BusinessGlosItem是父级的关系,而其中另一个是子级的关系-当然,我需要知道关系的类型也是如此。

这是到目前为止我得到的:

我的方法是:

    public BusinessGlosItem BusinessGlosItem { get; set; }

    public async Task<IActionResult> OnGetAsync(int? id)
    {

        BusinessGlosItem = await _context.businessGlosItem 
            .Include(x => x.BusinessArea)
            .Include(x => x.BusinessGlosItemStatus)
            .Include(x => x.ChildReferences)//here i want to include ReferenceType through ReferenceTypeId to get the name of the reference
            .Include(x => x.ParentReferences)//here i want to include ReferenceType through ReferenceTypeId to get the name of the reference
            .AsNoTracking()
            .SingleOrDefaultAsync(m => m.Id == id);

        return Page();
    }

我的域模型是:

public class BusinessGlosItem
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string BGIName { get; set; }

    [InverseProperty("ChildBGItem")]
    public ICollection<Reference> ChildReferences { get; set; }
    [InverseProperty("ParentBGItem")]
    public ICollection<Reference> ParentReferences { get; set; }

}

public class Reference
{
    public int Id { get; set; }
    [ForeignKey("ChildBGItem")]
    public int? ChildGlosItemId { get; set; }
    [ForeignKey("ParentBGItem")]
    public int? ParentGlosItemId { get; set; }

    public int ReferenceTypeId { get; set; }

    public virtual ReferenceType ReferenceType { get; set; }

    public virtual BusinessGlosItem ChildBGItem { get; set; }

    public virtual BusinessGlosItem ParentBGItem { get; set; }


}

public class ReferenceType
{
    public int Id { get; set; }
    public string ReferenceTypeName { get; set; }

    public ICollection<Reference> references { get; set; }

}

0 个答案:

没有答案