2013-02-14 49 views
0

當我運行代碼行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()); 

回答

5

您沒有映射目標類的所有成員。

呼叫Mapper.AssertConfigurationIsValid();有關該問題的詳細信息:

Unmapped members were found. Review the types and members below. 
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type 
============================================================== 
AccountUpdate -> User (Destination member list) 
ConsoleApplication1.AccountUpdate -> ConsoleApplication1.User (Destination member list) 
-------------------------------------------------------------- 
UserId 
Password 
Salt 
CreatedOn 
LastLogin 

爲了解決這個問題,明確忽略未映射的成員。


我只是測試這一點:

Mapper.CreateMap<AccountUpdate, User>() 
     .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()); 

Mapper.AssertConfigurationIsValid(); 

var update = new AccountUpdate 
{ 
    Email = "[email protected]", 
    Name = "The name", 
    Roles = "not important" 
}; 

var user = Mapper.Map<AccountUpdate, User>(update); 

Trace.Assert(user.Email == update.Email); 
Trace.Assert(user.Name == update.Name); 

這工作,也:

var user = new User(); 
Mapper.Map(update, user); 

Trace.Assert(user.Email == update.Email); 
Trace.Assert(user.Name == update.Name); 
+0

我試圖明確忽略未映射的成員,但是當我調用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

+0

我給答案增加了一個例子 - 它在這裏工作! – 2013-02-14 16:36:31

+0

Mapper.AssertConfigurationIsValid();不再拋出異常和像var user = Mapper.Map (update);將工作,但是當我調用var b = Mapper.Map (Account,User);我仍然得到一個例外。 – 2013-02-14 17:03:02

0

有一個問題,登錄,概述了一個潛在的解決方案的項目,這是將源對象用作映射,而不是目標。該original thread is here,以及相關的代碼示例是:

CreateMap<Source, Dest>(MemberList.Source) 

還有一個更通用的擴展方法有人在鏈接的問題,這也可能有助於節省時間寫。

相關問題