代码之家  ›  专栏  ›  技术社区  ›  xcelm

包括关联表中的数据

  •  1
  • xcelm  · 技术社区  · 6 年前

    我想问一下如何在我的案例中包含数据。我创建了一个关联表,定义了 BusinessGlosItems . 在下一步中,我创建了 商务词汇 并通过子关系和父关系包含引用表。

    最后我需要显示 商务词汇 所有相关数据。听起来很简单,但我不知道如何解决这个问题。有人能帮忙吗?

    我的域类如下:

    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; }
    }
    

    我想做的是:

    @page
    @model BusinessGlosItem.DetailsModel
    
    <div>
        <table class="table table-hover">
            <th>Reference Type</th>
            <th>Parent Item Name</th>
            <th>Child Item Name</th>
    
            @foreach (var item in Model.BusinessGlosItem.ParentReferences)
            {
                <tr>
                    <td>@item.ReferenceType.ReferenceTypeName</td>
                    <td>@item.ParentBGItem.BGIName</td>
                    <td>@item.ChildGlosItemId</td> //Here I would like to see the ChildItem Name
                </tr>
            }
        </table>
    </div>
    

    我得到 NullReferenceException: Object reference not set to an instance of an object. on the line 从这个角度来看。

    我的方法是:

    public BusinessGlosItem BusinessGlosItem { get; set; }
    
    public async Task<IActionResult> OnGetAsync(int? id)
    {
          if (id == null)
          {
                    return NotFound();
          }
    
          BusinessGlosItem = await _context.businessGlosItem                 
                    .Include(x => x.BusinessArea)
                    .Include(x => x.BusinessGlosItemStatus)
                    .Include(x => x.ChildReferences).ThenInclude(x=>x.ReferenceType)
                    .Include(x => x.ParentReferences).ThenInclude(x=>x.ReferenceType)
                    .Include(x=>x.businessGlosItemComments)
                    .SingleOrDefaultAsync(m => m.Id == id);
    
          if (BusinessGlosItem == null)
          {
               return NotFound();
          }
          return Page();
     }
    

    提前多谢!

    1 回复  |  直到 6 年前
        1
  •  2
  •   TanvirArjel    6 年前

    ThenInclude ChildBGItem ParentBGItem ChildReferences ParentReferences

    BusinessGlosItem = await _context.businessGlosItem                 
                .Include(x => x.BusinessArea)
                .Include(x => x.BusinessGlosItemStatus)
                .Include(x => x.ChildReferences)
                    .ThenInclude(x=>x.ReferenceType)
                .Include(x => x.ChildReferences) // You missed this include
                    .ThenInclude(x=>x.ChildBGItem)
                .Include(x => x.ChildReferences) // You missed this include
                    .ThenInclude(x=>x.ParentBGItem)
                .Include(x => x.ParentReferences)
                    .ThenInclude(x=>x.ReferenceType)
                .Include(x => x.ParentReferences) // You missed this include
                    .ThenInclude(x=>x.ChildBGItem)
                .Include(x => x.ParentReferences) // You missed this include
                    .ThenInclude(x=>x.ParentBGItem)
                .Include(x=>x.businessGlosItemComments)
                .SingleOrDefaultAsync(m => m.Id == id);