當我運行代碼行Mapper.Map(Account,User);我收到「缺少類型映射配置或不支持的映射」異常。我還想指出Mapper.Map(Account)這一行;不會拋出異常並返回預期的結果。我想要做的是將值從帳戶移動到用戶而不創建用戶的新實例。任何幫助都會很棒。謝謝!AutoMapper異常
public class AccountUpdate
{
[Email]
[Required]
public string Email { get; set; }
[Required]
[StringLength(25, MinimumLength = 3, ErrorMessage = "Your name must be between 3 and 25 characters")]
public string Name { get; set; }
public string Roles { get; set; }
}
public class User
{
public User()
{
Roles = new List<Role>();
}
public int UserId { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public byte[] Password { get; set; }
public byte[] Salt { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime LastLogin { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
Mapper.CreateMap<AccountUpdate, User>().ForMember(d => d.Roles, s => s.Ignore());
我試圖明確忽略未映射的成員,但是當我調用Mapper.AssertConfigurationIsValid();我得到了上面列出的同樣的東西。我的配置現在看起來像這樣:Mapper.CreateMap()。ForMember(d => d.Roles,s => s.Ignore()) .ForMember(d => d.UserId,s => s.Ignore()) .ForMember(d => d.Password,s => s.Ignore()) .ForMember(d => d.Salt,s => s.Ignore()) .ForMember d => d.CreatedOn,s => s.Ignore()) .ForMember(d => d.LastLogin,s => s.Ignore()); –
2013-02-14 16:28:23
我給答案增加了一個例子 - 它在這裏工作! – 2013-02-14 16:36:31
Mapper.AssertConfigurationIsValid();不再拋出異常和像var user = Mapper.Map(update);將工作,但是當我調用var b = Mapper.Map (Account,User);我仍然得到一個例外。 –
2013-02-14 17:03:02