Error in getting navigation property in a many to many relationship in entity framework core

asp.net-core-mvc c# entity-framework-core many-to-many navigation-properties

Question

I have two entities that are connected in a many to many relationship as follows

public class Fee
{
    public int Id { get; set; }
    [Required]
    public string Label { get; set; }
    public string Description { get; set; }

   ----Ignored for brevity----

    public virtual ICollection<ClassFee> ClassFees { get; set; }

}

public class StudentClass
{

    public int Id { get; set; }
    public string Label { get; set; }
    ---Ignored for brevity---
    public virtual ICollection<ClassFee> ClassFees { get; set; }

}

 public class ClassFee
{
    public int Id { get; set; }
    [Display(Name ="Fee")]
    public int FeeId { get; set; }
    [Display(Name ="Class")]
    public int ClassId { get; set; }

    ---ignored for brevity---

    [ForeignKey("FeeId")]
    public Fee Fee { get; set; }

    [ForeignKey("ClassId")]
    public StudentClass Class { get; set; }
}

The following is my FeesController class where I intend to get the details of a given fee and the list of classes the fee is applied to

public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var fee = await _context.Fees
            .Include(cf => cf.ClassFees)
            .FirstOrDefaultAsync(m => m.Id == id);
        if (fee == null)
        {
            return NotFound();
        }

        return View(fee);
    }

I was expecting that with this, I should be able to have the class for the fees in the view by calling classFeeEntityInstance.class.label to get the label for the class but it returns null. Also when I put a breakpoint on the method and run the code, the classfee.class is returned as null

I have also tried to do the following to see if I could eagerly load the class in the query but it does not seen to be possible to find the class from the classfee with the ThenInclude caluse as follows

.Include(cf => cf.ClassFees).ThenInclude(f=>f.Class)

but the f.Class does not exist at that point because the intellisence in Visual Studio does not suggest it and it underlines it immediately I try to add it.

Below is how I wish to use the class under the classFees in my view

@foreach (ClassFee classFee in Model.ClassFees)
{
   @classFee.Class.Label
}

But the Class.Label throws up a null reference exception when the code is run

The application is being built on ASP.NET-Core 3.1 with Entity Framework 3.1

I will appreciate any guide to resolve this Thank you

1
1
3/19/2020 2:00:46 PM

Accepted Answer

Go ahead and try to build it using ThenInclude, it should work. There is a known issue with Intellisense and ThenInclude.

0
3/19/2020 2:51:48 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