EF Core related entities only returns one record in a one-to-many relationship

Espi 1 Reputation point
2021-06-03T06:32:41.2+00:00

I have an app that lists Computers in a domain. Each computer has a one-to-many relationship to an Applications table. I am able to pull the Computer details, but when I map try to pull the Applications associated with the computer I only get one returned. Here is my setup:

Models

Public class computers  
  
        [Key]  
            public int computerid { get; set; }  
            public ICollection<applications> applications { get; set; }  
            public ICollection<hotfixes> hotfixes { get; set; }  
      
    Public class applications  
            [ForeignKey ("computerid")]  
            public computers computers { get; set; }  
      
    Public class hotfixes  
            [ForeignKey ("computerid")]  
            public computers computers { get; set; }  
  
**Details.cshtml.cs**  
         public computers computers { get; set; }  
  
         public async Task<IActionResult> OnGetAsync(int? id)  
         {  
            if (id == null)  
            {  
                return NotFound();  
            }  
  
            computers = await _context.Computers  
                        .Include(a => a.applications)  
                        .Include(h => h.hotfixes)  
                        .AsNoTracking()  
                        .AsSingleQuery()  
                        .FirstOrDefaultAsync(m => m.id == id);  
  
           if (computers == null)  
           {  
               return NotFound();  
           }  
               return Page();  
  
        }  
  
  
**Details.cshtml**  
  
section calling the related applications records - ONLY ONE APPLICATION RECORD IS RETURNED  
  
 @foreach (var application in Model.computers.applications)  
                      
            {  
             <tr>  
              <td width="25%" align="center">@Html.DisplayFor(modelapplication => application.name)</td>  
              <td width="25%" align="center">@Html.DisplayFor(modelapplication => application.version)</td>  
              <td width="25%" align="center">@Html.DisplayFor(modelapplication => application.publisher)</td>  
              <td width="25%" align="center">@Html.DisplayFor(modelapplication => application.installdate)</td>  
              </tr>    
             }  
  
  
section calling the related hotfixes records - ONLY ONE HOTIX RECORD IS RETURNED  
  
@foreach (var hotfix in Model.computers.hotfixes)  
              {  
               <tr>  
               <td width="20%" align="center">@Html.DisplayFor(modelItem => hotfix.name)</td>  
                <td width="20%" align="center">@Html.DisplayFor(modelItem => hotfix.helplink)</td>  
                <td width="20%" align="center">@Html.DisplayFor(modelItem => hotfix.description)</td>  
                <td width="20%" align="center">@Html.DisplayFor(modelItem => hotfix.installedon)</td>  
                <td width="20%" align="center">@Html.DisplayFor(modelItem => hotfix.installedby)</td>  
                </tr>  
               }  

I cannot seem to be able to make it work no matter what I do. Any guidance would be appreciated.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
779 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.