私はフラットビューモデルにマップする必要がある以下のDTOを持っていますが、要求から来るプロパティのいくつかは共有されていますが、存在する可能性があります来る名前のリスト。AutoMapperからDTOのリストから個々のオブジェクトへ
public class ShinyDTO
{
public List<UserDetails> Users { get; set; }
public string SharedPropertyOne { get; set; }
public string SharedPropertyTwo { get; set; }
}
public class UserDetails
{
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
}
public class MyRealClass
{
public string SharedPropertyOne {get;set;}
public string SharedPropertyTwo {get;set;}
public string Title {get;set;}
public string Forename {get;set;}
public string Surname {get;set;}
}
//This will map all the shared properties
MyRealClass request = Mapper.Map<MyRealClass>(dto);
foreach (var record in dto.Users){
//This bit overwrites the properties set above and then I only have the properties set for Forename, Surname, etc...
request = Mapper.Map<MyRealClass>(record);
}
これをMyRealClassのリストにマップする必要があります。私は別々のマッピングを作成してforeach内でループすることを試みましたが、これにより初期属性が削除され続けました。
上記のプロパティを無視するように2番目のマッピングを設定しようとしましたが、これを取得できず、プロパティを上書きしていました。
var autoMapperConfiguration = new MapperConfigurationExpression();
autoMapperConfiguration
.CreateMap<MyRealClass, UserDetails>()
.ForMember(c => c.SharedPropertyOne, d => d.Ignore())
.ForMember(c => c.SharedPropertyTwo, d => d.Ignore());
これは私が必要としていたものです。あなたはマッピングについて正しくありました。これは手作業で入力したものでしたが、コピーして貼り付けていませんでした。 私はmapped = Mapper.Map(record、mapped)を実行しようとしていました。それを再割り当てする。 – Kieran