title: Trying to read from database table, but output is incorrect
body: <p>Here is my code:</p>
  <p><strong>tracks.cshtml</strong></p>
  <pre><code>@page
  @using Project.Models
  @model Project.Pages.TracksModel
  @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
  
  &lt;div class=&quot;row&quot;&gt;
      &lt;h1 class=&quot;display-2&quot;&gt;Tracks&lt;/h1&gt;
      &lt;table class=&quot;table&quot;&gt;
          &lt;thead class=&quot;thead-inverse&quot;&gt;
              &lt;tr&gt;
                  &lt;th&gt;Artist name&lt;/th&gt;
                  &lt;th&gt;Album name&lt;/th&gt;
                  &lt;th&gt;Track ID&lt;/th&gt;
                  &lt;th&gt;Track name&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
  
          &lt;tbody&gt;
              @foreach (Track track in Model.Tracks)
              {
                  &lt;tr&gt;
                      &lt;td&gt;@Html.DisplayFor(modelArtists =&gt; track.Artist.Name)&lt;/td&gt;
                      &lt;td&gt;@Html.DisplayFor(modelAlbums =&gt; track.Album.Title)&lt;/td&gt;
                      &lt;td&gt;@track.TrackId&lt;/td&gt;
                      &lt;td&gt;@track.Name&lt;/td&gt;
                      &lt;td&gt;&lt;a asp-page=&quot;/Tracks/Details&quot; asp-route-id=&quot;@track.TrackId&quot;&gt;Details&lt;/a&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;a asp-page=&quot;/Tracks/Edit&quot; asp-route-id=&quot;@track.TrackId&quot;&gt;Edit&lt;/a&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;a asp-page=&quot;/Tracks/Delete&quot; asp-route-id=&quot;@track.TrackId&quot;&gt;Delete&lt;/a&gt;&lt;/td&gt;
                  &lt;/tr&gt;
              }
          &lt;/tbody&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  &lt;div class=&quot;row&quot;&gt;
      &lt;p&gt;Enter a name &amp; TrackID (over 3510) for a new track:&amp;nbsp;&lt;/p&gt;
      &lt;form method=&quot;POST&quot;&gt;
          &lt;div&gt;&lt;input asp-for=&quot;Track.Name&quot; /&gt;&lt;/div&gt;
          &lt;div&gt;&lt;input asp-for=&quot;Track.TrackId&quot; /&gt;&lt;/div&gt;
          &lt;input type=&quot;submit&quot; /&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;a asp-page=&quot;/Index&quot;&gt;Home&lt;/a&gt;
  &lt;/div&gt;
  </code></pre>
  <p><strong>tracks.cshtml.cs</strong></p>
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using Microsoft.AspNetCore.Mvc.RazorPages;
  using Microsoft.EntityFrameworkCore;
  using Project.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  
  namespace Project.Pages
  {
      public class TracksModel : PageModel
      {
          private Chinook db;
  
          public TracksModel(Chinook injectedContext)
          {
              db = injectedContext;
          }
          public IEnumerable&lt;Track&gt; Tracks { get; set; }
          public void OnGetAsync()
          {
              ViewData[&quot;Title&quot;] = &quot;Chinook Web Site - Tracks&quot;;
              Tracks = db.Tracks.Include(a =&gt; a.Album)
              .Include(a =&gt; a.Artist);
          }
          [BindProperty]
          public Track Track { get; set; }
  
          public IActionResult OnPost()
          {
              if (ModelState.IsValid)
              {
                  db.Tracks.Add(Track);
                  db.SaveChanges();
                  return RedirectToPage(&quot;/tracks&quot;);
              }
              return Page();
          }
  
          public IActionResult DeleteTrack(int TrackId)
          {
              var track = db.Tracks.Find(TrackId);
  
              if (track == null) return Page();
  
              db.Tracks.Remove(track); db.SaveChanges(); return RedirectToPage(&quot;/tracks&quot;);
          }
      }
  }
  </code></pre>
  <p><a href="https://github.com/double-happ1ness/chinook" rel="nofollow noreferrer">GitHub repo</a></p>
  <p>There is something wrong as the page appears to repeat the artist names multiple times, to such an extent that all the artists names that are displayed begin with the letter A. I should say that the way I have the database set up, <code>ArtistId</code> is a foreign key of the <code>Album</code> table, while <code>AlbumId</code> is a foreign key of the <code>Tracks</code> table.</p>
  <p>Maybe I need clauses like <code>.Where(a =&gt; a.AlbumId == Tracks.AlbumId)</code> and <code>.Where(a =&gt; a.ArtistId == Album.ArtistId)</code>? I tried to write something like that, but I must have done it wrong as it just stopped the tracks from displaying altogether. Any help please? TIA</p>
  <p>Edit: I'm noticing that whereas my Album titles are joined from the <code>Album</code> table, my Artist names are not, even though the method is essentially just the same. It occurs to me that whereas the <code>Album</code> table is directly connected to the <code>Track</code> table (through the <code>AlbumId</code> foreign key), the <code>Artist</code> table is not. It is only indirectly related to the 'Track' table, through the <code>Album</code> table`.</p>
  <p><code>Artist</code>----<code>Album</code>----<code>Track</code></p>
  <p>Edit2: I was told that <code>Where</code> is not needed in this instance, and that I should use 'ThenInclude`, so I've currently got</p>
  <pre><code>   public void OnGetAsync()
      {
          ViewData[&quot;Title&quot;] = &quot;Chinook Web Site - Tracks&quot;;
          Tracks = db.Tracks.Include(a =&gt; a.Album).ThenInclude(b =&gt; b.Artist).ToList();
      }
  </code></pre>
  <p>It's still resulting in the exact same error though.</p>