【发布时间】:2021-02-23 14:03:47
【问题描述】:
我正在制作一个网络应用程序,用户可以在其中创建播放列表和添加音乐(如 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();
}
我错过了什么?
【问题讨论】:
标签: c# asp.net-mvc entity-framework-core entity-framework-6