无法使用外键获取记录

Unable to get records using a foreign key

我正在制作一个网络应用程序,用户可以在其中创建播放列表和添加音乐(如 Spotify、Deezer 等)。但是我无法在播放列表中获取音乐。这是我的播放列表和音乐 类:

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

我尝试遵循许多指南,包括 this one by Microsoft 但在加载显示页面时我无法获得具有相同 PlaylistId 的所有音乐。我使用此功能检索我的播放列表数据:

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

我错过了什么?

List<Musics> 集合实际上并不直接存储在您的数据库中。而是通过 playlistIduserId.

查询播放列表中的所有音乐
public async Task<IEnumerable<Music>> GetPlaylistsOf(int userId, int playlistId)
{
    return await _db.Musics
        .Include(p => p.Playlist)
        .Where(p => p.Playlist.UserId == userId)
        .OrderByDescending(p => p.PlaylistId)
        .ToListAsync();
}

我假设你需要这个:

public Task<IEnumerable<Music>> GetUserMusic(int userId)
{
    return _db.Playlists
        .Where(p => p.UserId == userId)
        .SelectMany(p => p.Musics)
        .ToListAsync();
}

好吧,这个太傻了。

当我看到 Qudus 和 Svyatoslav Danyliv 的答案时,我意识到我一直在编辑函数 GetPlaylistsOf(int userId) 而不是完全不同的 GetPlaylistById(int playlistId) 。所以这现在工作得很好:

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

谢谢你帮助我