AutoMapper映射导航属性问题

时间:2016-03-30 19:55:59

标签: c# asp.net-mvc automapper

我已按照此回复AutoMapper mapping int[] or List<int> from ViewModel to a List<Type> in Domain Model并在CategoryId上将SelectedCategoryIdsUploadSongViewModel绑定,但我还要将其绑定到SongId Id上的UploadSongViewModelnamespace 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; } }

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

我真的不明白自动映射器代码在Darin Dimitrov的回答中做了什么,所以很难调试。如果有人能够解释这个映射对于categoryId是如何工作的,以及为什么它不适用于SongId会很棒。

我得到的例外是:

  

System.Collections.Generic.List上的以下属性 int countrow = firstRow + rowCount; SqlStatement = "SELECT * FROM CUSTOMERS ORDER BY %S %S offset ? limit ? "; ps = conn.prepareStatement(sql); ps.setInt(1, countrow); ps.setInt(2, firstRow); 1 [[SoundVast.Models.SongCategory,SoundVast,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]。   语境:       映射到属性SongCategories从System.Int32到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   类型&#39; AutoMapper.AutoMapperConfigurationException&#39;的例外情况被扔了。

1 个答案:

答案 0 :(得分:1)

一旦应用了基本规则,这可以通过一些自定义映射轻松实现:

function getChildrenProperty(object) {
  for (var property in object) {
    if (object.hasOwnProperty(property)) {
      if (property.toLowerCase().indexOf("children") > -1) {
        return property;
      }
    }
  }

  return null;
}