Automapper,缺少类型映射配置或不支持的映射错误

Automapper , Missing type map configuration or unsupported mapping error

电影

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string MovieName { get; set; }
        public int Year { get; set; }
        public decimal Price { get; set; }


        public int MovieTypeId { get; set; }
        public MovieType MovieType { get; set; }

        public int DirectorId { get; set; }
        public Director Director { get; set; }

        public ICollection<MovieActor> MovieActors { get; set; }
      

        public bool IsActive { get; set; } = true;

MovieCreateModel

  CreateMap<MovieCreateModel, Movie>()
                    .ForMember(src => src.MovieName, opt => opt.MapFrom(x => x.MovieName))
                    .ForMember(src => src.Price, opt => opt.MapFrom(x => x.Price))
                    .ForMember(src => src.Year, opt => opt.MapFrom(x => x.Year))
                    .ForMember(src => src.MovieTypeId, opt => opt.MapFrom(x => x.MovieTypeId))
                    .ForMember(src => src.DirectorId, opt => opt.MapFrom(x => x.DirectorId))
                    .ForMember(src => src.MovieActors, opt => opt.Ignore());

var map = _mapper.Map<Movie>(CreateModel);

我正在尝试将 MovieCreateModel 映射到 Movie,但我收到此错误:

Missing type map configuration or unsupported mapping

调试屏幕

Create Model

我错在哪里?谢谢。

public class MovieCreateCommand
    {
        private readonly IMovieStoreDbContext _context;
        private readonly IMapper _mapper;

        public MovieCreateModel CreateModel { get; set; }

        public MovieCreateCommand(IMovieStoreDbContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        public void Handle()
        {
            Movie movie = new Movie
            {
                DirectorId = CreateModel.DirectorId,
                IsActive = true,
                MovieName = CreateModel.MovieName,
                MovieTypeId = CreateModel.MovieTypeId,
                Price = CreateModel.Price,
                Year = CreateModel.Year,
                MovieActors = new List<MovieActor>()
            };

            // var movie = _mapper.Map<Movie>(CreateModel);

            foreach (var actorId in CreateModel.MovieActorsId)
            {
                var actorResult = _context.Actors.SingleOrDefault(p => p.Id == actorId);
                var directorResult = _context.Directors.SingleOrDefault(p => p.Id == CreateModel.DirectorId);

                if (actorResult is not null && directorResult is not null)
                {
                    movie.MovieActors.Add(new MovieActor { Actors = actorResult, Movie = movie });
                }
                else if (actorResult is null)
                {
                    throw new InvalidOperationException("Aktör bulunamadı.");
                }
                else if (directorResult is null)
                {
                    throw new InvalidOperationException("Yönetmen bulunamadı.");
                }
            }
            _context.Movies.Add(movie);
            _context.SaveChanges();

        }
        

        public class MovieCreateModel
        {
            public string MovieName { get; set; }
            public decimal Price { get; set; }
            public int Year { get; set; }

            public int MovieTypeId { get; set; }

            public int DirectorId { get; set; }

            public ICollection<int> MovieActorsId { get; set; }

        }
}

我发现我的错误,MovieActor 列表为空,所以这导致了错误。我在没有自动映射器的情况下解决了问题。

我可以在使用 Authomapper 映射时创建 List 对象吗?

不需要第三方依赖的替代方法 ;)

public static Movie ToMovie(this MovieCreateModel model)
{
    return new Movie 
    {
        Name = model.MovieName,
        Price = model.Price,
        Year = model.Year,
        DirectorId = model.DirectoryId,
        TypeId = model.MovieTypeId
    };
}

// Usage
var entity = newMovieModel.ToMovie();

请注意,此方法没有映射限制;)