C# 自动映射两个对象,第二个对象覆盖第一个对象';s属性
我试过这个答案:。但是,我的第二个mappingC# 自动映射两个对象,第二个对象覆盖第一个对象';s属性,c#,asp.net-mvc,automapper,C#,Asp.net Mvc,Automapper,我试过这个答案:。但是,我的第二个mappingmapper.Map(additionalUploadSongViewModel,songs)仍在用默认值覆盖其他字段,但它正确映射了BuyLink和歌词 namespace App { public static class AutoMapper { public static MapperConfiguration Config; public static void Initialize()
mapper.Map(additionalUploadSongViewModel,songs)代码>仍在用默认值覆盖其他字段,但它正确映射了BuyLink
和歌词
namespace App
{
public static class AutoMapper
{
public static MapperConfiguration Config;
public static void Initialize()
{
//Removed other mappings for verbose
Config = new MapperConfiguration(x => {
x.CreateMap<UploadSongViewModel, Song>()
.IgnoreAllNonExisting()
.ForMember(a => a.AudioName, m => m.MapFrom(s => s.SongName))
.ForMember(a => a.AudioPath, m => m.MapFrom(s => s.SongPath));
x.CreateMap<AdditionalUploadSongViewModel, Song>()
.IgnoreAllNonExisting();
});
//No errors here
Config.AssertConfigurationIsValid();
}
}
}
域模型:
public abstract class Entity
{
public int Id { get; set; }
}
public abstract class Audio : Entity
{
public string AudioName { get; set; }
public string AudioPath { get; set; }
public string CoverImagePath { get; set; }
}
public class Song : Audio
{
//Removed some for verbose
public string AlbumName { get; set; }
public string ArtistName { get; set; }
public string Lyrics { get; set; }
public string BuyLink { get; set; }
}
视图模型:
public class UploadSongViewModel
{
public int Id { get; set; }
public string SongName { get; set; }
public string ArtistName { get; set; }
public string AlbumName { get; set; }
public string SongPath { get; set; }
public string CoverImagePath { get; set; }
}
public class AdditionalUploadSongViewModel
{
public string BuyLink { get; set; }
public string Lyrics { get; set; }
}
控制器:
[HttpPost]
public async Task<ActionResult> UploadMusic(IList<UploadSongViewModel> uploadSongsViewModel, IList<AdditionalUploadSongViewModel> additionalUploadSongViewModel)
{
//uploadSongViewModel and additionalUploadSongViewModel has all properties filled out properly here.
IMapper mapper = AutoMapper.Config.CreateMapper();
List<Song> songs = mapper.Map<List<UploadSongViewModel>, List<Song> (uploadSongsViewModel.ToList());
//songs now have proper mapped properties from uploadSongsViewModel
mapper.Map(additionalUploadSongViewModel, songs);
//all songs properties (songName, artistName etc) have now been overwritten by additionalUploadSongViewModel properties, instead of only BuyLink and Lyrics.
}
[HttpPost]
公共异步任务UploadMusic(IList uploadSongsViewModel,IList additionalUploadSongViewModel)
{
//uploadSongViewModel和additionalUploadSongViewModel在此处正确填写了所有属性。
IMapper mapper=AutoMapper.Config.CreateMapper();
List songs=mapper.Map..这是一个奇怪的想法,使用两个独立的集合..你怎么知道集合1中的项目2每次都与集合2中的项目2匹配?@JamieD77谢谢你的链接。似乎这是我的问题。我不明白你的后续问题。我只是想知道如果上传songsviewm会发生什么odel
有10项,而additionalUploadSongViewModel
只有9项。@JamieD77啊。它不能。它们的数量必须相同。你有没有想过把additionalUploadSongViewModel
作为UploadSongViewModel
的一个属性?Automapper可以处理这个问题
[HttpPost]
public async Task<ActionResult> UploadMusic(IList<UploadSongViewModel> uploadSongsViewModel, IList<AdditionalUploadSongViewModel> additionalUploadSongViewModel)
{
//uploadSongViewModel and additionalUploadSongViewModel has all properties filled out properly here.
IMapper mapper = AutoMapper.Config.CreateMapper();
List<Song> songs = mapper.Map<List<UploadSongViewModel>, List<Song> (uploadSongsViewModel.ToList());
//songs now have proper mapped properties from uploadSongsViewModel
mapper.Map(additionalUploadSongViewModel, songs);
//all songs properties (songName, artistName etc) have now been overwritten by additionalUploadSongViewModel properties, instead of only BuyLink and Lyrics.
}