简体   繁体   English

AutoMapper映射导航属性问题

[英]AutoMapper Mapping navigation property issue

I'm following this answer AutoMapper mapping int[] or List<int> from ViewModel to a List<Type> in Domain Model and have got it binding with my CategoryId to SelectedCategoryIds on UploadSongViewModel , however I want to also get it to bind SongId to Id on UploadSongViewModel . 我下面这个答案在域模型AutoMapper映射INT []或列表<int>的从视图模型到一个List <类型>并得到了它与我结合CategoryIdSelectedCategoryIdsUploadSongViewModel ,不过我想也把它绑定SongIdIdUploadSongViewModel

namespace App
{
    public static class AutoMapper
    {
        public static MapperConfiguration Config;

        public static void Initialize()
        {
            //It works with CategoryId, but throws an error when I include SondId.
            Config = new MapperConfiguration(x =>
            {
                x.CreateMap<int, SongCategory>()
                    .IgnoreAllNonExisting()
                    .ForMember(dest => dest.CategoryId, opt => opt.MapFrom(src => src))
                    .ForMember(dest => dest.SongId, opt => opt.MapFrom(src => src)); //Code that's throwing exception

                x.CreateMap<UploadSongViewModel, Song>()
                    .IgnoreAllNonExisting()
                    .ForMember(dest => dest.AudioName, opt => opt.MapFrom(src => src.SongName))
                    .ForMember(dest => dest.AudioPath, opt => opt.MapFrom(src => src.SongPath))
                    .ForMember(dest => dest.SongCategories, opt => opt.MapFrom(src => src.SelectedCategoryIds))
                    .ForMember(dest => dest.SongCategories, opt => opt.MapFrom(src => src.Id)); //Code that's throwing exception
            });

            //Throws exception here because it can't bind SongId property
            Config.AssertConfigurationIsValid(); 
        }
    }
}

public abstract class Entity
{
    public int Id { get; set; }
}

public class SongCategory : Entity
{
    public int SongId { get; set; }
    public int CategoryId { get; set; }
}

public class Song : Audio
{
    public string AlbumName { get; set; }
    public string ArtistName { get; set; }

    public List<SongCategory> SongCategories { get; set;}
}

public class UploadSongViewModel
{
    public int Id { get; set; }
    public string ArtistName { get; set; }
    public string AlbumName { get; set; }
    public int[] SelectedCategoryIds { get; set; }

    public MultiSelectList CategoriesSelectList { get; set; }
}

I don't really understand what the auto mapper code is doing from Darin Dimitrov answer so it's hard to debug. 我不太了解Darin Dimitrov回答中的自动映射器代码在做什么,因此很难调试。 If someone could explain how this mapping works for categoryId and why it is not working for SongId that would be great. 如果有人可以解释此映射如何适用于categoryId,以及为什么它不适用于SongId则很好。

The exception I get is: 我得到的异常是:

The following property on System.Collections.Generic.List 1[[SoundVast.Models.SongCategory, SoundVast, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] cannot be mapped: SongCategories Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type System.Collections.Generic.List 1[[SoundVast.Models.SongCategory, SoundVast, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. System.Collections.Generic.List 1[[SoundVast.Models.SongCategory, SoundVast, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] cannot be mapped: SongCategories Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type System.Collections.Generic.List上的以下属性1[[SoundVast.Models.SongCategory, SoundVast, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] cannot be mapped: SongCategories Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type System.Collections.Generic.List 1 [[SoundVast.Models.SongCategory,SoundVast,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]。 Context: Mapping to property SongCategories from System.Int32 to System.Collections.Generic.List`1[[SoundVast.Models.SongCategory, SoundVast, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Mapping from type SoundVast.Areas.Upload.Models.UploadSongViewModel to SoundVast.Models.Song Exception of type 'AutoMapper.AutoMapperConfigurationException' was thrown. 上下文:从System.Int32映射到属性SongCategories到System.Collections.Generic.List`1 [[SoundVast.Models.SongCategory,SoundVast,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]从类型SoundVast映射。将Areas.Upload.Models.UploadSongViewModel更改为SoundVast.Models.Song引发了类型为'AutoMapper.AutoMapperConfigurationException'的异常。

This can be easily achieved with some custom mapping once the basic rules have been applied: 一旦应用了基本规则,就可以通过一些自定义映射轻松实现:

x.CreateMap<int, SongCategory>()
    .IgnoreAllNonExisting()
    .ForMember(dest => dest.CategoryId, opt => opt.MapFrom(src => src));

x.CreateMap<UploadSongViewModel, Song>()
    .IgnoreAllNonExisting()
    .ForMember(dest => dest.AudioName, opt => opt.MapFrom(src => src.SongName))
    .ForMember(dest => dest.AudioPath, opt => opt.MapFrom(src => src.SongPath))
    .ForMember(dest => dest.SongCategories, opt => opt.MapFrom(src => src.SelectedCategoryIds))
    .AfterMap((viewModel, model) => 
    {
        foreach (var item in model.SongCategories)
        {
            item.Id = viewModel.Id;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM