Question

I would like to ask how to Include data in my case. I've created an associative table where I define relationship between BusinessGlosItems. In the next step I've created model of BusinessGlosItems and included Reference table through Child and Parent relationships.

Finally I need to display the details of BusinessGlosItems with its all associated data. It sounds easy, but I don't know how to solve this. Can anybody help please?

My Domain Classes are as follows:

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

What I would like to do:

@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>

I am getting NullReferenceException: Object reference not set to an instance of an object. on the line from the view.

My method is:

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

Thanks a lot in advance!

1
1
2/10/2019 10:43:42 PM

Accepted Answer

You forgot to mention ThenInclude for both ChildBGItem and ParentBGItem in both ChildReferences and ParentReferences. So your query should be as follows:

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

Now it should work.

2
2/9/2019 12:57:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow