Search code examples
c#asp.net-mvcentity-framework-coreentity-framework-6

Unable to get records using a foreign key


I'm making a web application where users can create playlists and add musics (like Spotify, Deezer, etc.). But I can't manage to get musics in a playlist. Over here are my Playlist and Music classes:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Melodie.Models
{
    public class Playlist
    {
        [Key, Column("playlist_id")]
        public int? PlaylistId { get; set; }
        
        
        [Required, Column("user_id")]
        public int? UserId { get; set; }
        
        
        [Required, Column("color_id")]
        public int? ColorId { get; set; }
        
        
        [Required, Column("name"), MaxLength(100)]
        public string Name { get; set; }
        
        
        [DataType(DataType.MultilineText), Column("Description"), MaxLength(255)]
        public string Description { get; set; }
        
        
        public List<Music> Musics { get; set; }

        public Playlist()
        {
            UserId = 1;
            ColorId = 1;
            Name = "Nouvelle playlist";
            //Musics = new List<Music>();
        }
    }
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Http;

namespace Melodie.Models
{
    public class Music
    {
        [Key, Column("music_id")]
        public int? MusicId { get; set; }
        
        
        [Required, Column("playlist_id")]
        public int? PlaylistId { get; set; }
        //[ForeignKey("playlist_id")]
        //public virtual Playlist Playlist { get; set; }
        public Playlist Playlist { get; set; }
        
        
        [Required, Column("name"), MaxLength(100)]
        public string Name { get; set; }
        
        
        [Required, Column("file_path"), MaxLength(2048)]
        public string FilePath { get; set; }
        
        
        [NotMapped]
        public IFormFile MusicFile { get; set; }  
    }
}

I tried to follow many guides, including this one by Microsoft but I can't get all musics that have the same PlaylistId when loading the display page. I retrieve my playlist data using this function:

public async Task<IEnumerable<Playlist>> GetPlaylistsOf(int userId)
{
    return await _db.Playlists
        .Where(p => p.UserId == userId)
        .Include(p => p.Musics)
        .OrderByDescending(p => p.PlaylistId)
        .ToListAsync();
}

What am I missing?


Solution

  • Well, this one was pretty foolish.

    When I saw the answers of Qudus and Svyatoslav Danyliv, I realized that all this time I was editing the function GetPlaylistsOf(int userId) instead of GetPlaylistById(int playlistId) which are completly differrent. So this is now working perfectly:

    public async Task<Playlist> GetPlaylistById(int playlistId)
    {
        return await _db.Playlists
            .Include(p => p.Musics)
            .FirstOrDefaultAsync(p => p.PlaylistId == playlistId);
    }
            
    public async Task<IEnumerable<Playlist>> GetPlaylistsOf(int userId)
    {
        return await _db.Playlists
            .Where(p => p.UserId == userId)
            .OrderByDescending(p => p.PlaylistId)
            .ToListAsync();
    }
    

    Thanks for helping me