.Net Core Web Api / HttpGet / 关于获取 ICollection

.Net Core Web Api / HttpGet / About Get ICollection

首先,对不起我的英语,它并不完美。我希望我能解释我的问题。

WebApi 有像这样的电影Class

    public class Movie
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime ReleaseDate { get; set; }
        public float Price { get; set; }
        public int DirectorID { get; set; }
        public Director Director { get; set; }
        public int GenreID { get; set; }
        public Genre Genre { get; set; }
        public ICollection<Actor> Actors { get; set; }
    }

当我使用 HttpGet 电影时 it happens

它无法读取演员。我该如何解决这个问题?

GetMoviesQuery

namespace WebApi.Application.MovieOperations.Queries.GetMovies
{
    public class GetMoviesQuery
    {
        private readonly IMovieStoreDbContext _context;
        private readonly IMapper _mapper;
        public GetMoviesQuery(IMovieStoreDbContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        public List<GetMoviesModel> Handle()
        {
            var _movieList = _context.Movies.Include(x => x.Genre).Include(y => y.Director).OrderBy(z => z.Id).ToList<Movie>();
            List<GetMoviesModel> mv = _mapper.Map<List<GetMoviesModel>>(_movieList);
            return mv;
        }
    }
    public class GetMoviesModel
    {
        public string Name { get; set; }
        public string ReleaseDate { get; set; }
        public string Genre { get; set; }
        public string Director { get; set; }
        public float Price { get; set; }
        public ICollection<Actor> Actors { get; set; }
    }
}

谢谢。

你也必须包括演员

var _movieList = _context.Movies
                   .Include(x => x.Actors)
                    .Include(x => x.Genre)
                     .Include(y => y.Director)
                      .OrderBy(z => z.Id)
                      .ToList();

更新:

如果您只需要演员的名字和姓氏,您将不得不创建一个模型class

public class ActorViewModel
{
 public string Name { get; set; }
 public string SurName { get; set; }
}

并相应地修复 GetMoviesModel

 public class GetMoviesModel
    {
        public string Name { get; set; }
       .....
        public ICollection<ActorViewModel> Actors { get; set; }
    }