2012-09-15 136 views
3

我已經看過各種類似的帖子,但不能發現我的方式與這一個錯誤。基本上我有兩個視圖更新「設置」對象的不同部分。視圖模型包含兩個屬性中的一個,並取決於正在設置的屬性,應該忽略另一個。當它是ViewModel ==> Entity direct時它工作正常,但當Automapper嘗試更新它失敗的嵌套對象時。Automapper不會忽略嵌套屬性

我有以下目的的結構:

public class Account 
{ 
    public int AccountId { get; set; } 
    public DateTime DateToBeIgnored { get; set; } 
    public AccountSetting Settings { get; set; } 
} 

public class AccountSetting 
{ 
    public string PropertyOne { get; set; } 
    public string PropertyTwo { get; set; } 
} 

public class AccountViewModel 
{ 
    public int AccountId { get; set; } 
    public DateTime DateToBeIgnored { get; set; } 
    public AccountSettingViewModel Settings { get; set; } 
} 

public class AccountSettingViewModel 
{ 
    public string PropertyTwo { get; set; } 
} 

public class OtherAccountSettingViewModel 
{ 
    public string PropertyOne { get; set; } 
} 

隨着映射:

void WireUpMappings() 
{ 
    // From the entities to the view models 
    Mapper.CreateMap<Account, AccountViewModel>(); 
    Mapper.CreateMap<AccountSetting, AccountSettingViewModel>(); 
    Mapper.CreateMap<AccountSetting, OtherAccountSettingViewModel>(); 

    // From the view models to the entities 
    Mapper.CreateMap<AccountViewModel, Account>() 
     .ForMember(dest => dest.DateToBeIgnored, opt => opt.Ignore()); 

    Mapper.CreateMap<AccountSettingViewModel, AccountSetting>() 
     .ForMember(dest => dest.PropertyTwo, opt => opt.Ignore()); 

    Mapper.CreateMap<OtherAccountSettingViewModel, AccountSetting>() 
     .ForMember(dest => dest.PropertyOne, opt => opt.Ignore()); 

    Mapper.AssertConfigurationIsValid(); 
} 

當映射[OtherAccountSettingViewModel - > AccountSetting],只有屬性 「PropertyTwo」 被分配(以及「PropertyOne」的原始價值保持不變) - 這是我所期望的。

但是,如果映射[AccountViewModel - > Account]「DateToBeIgnored」被忽略,而Account.AccountSetting.PropertyTwo的前一個值被替換爲「null」。

任何人都可以發現我的方式的錯誤?

回答

2

這裏是解決方案:

[TestMethod] 
public void TestMethod1() 
{ 
    Mapper.CreateMap<AccountViewModel, Account>() 
     .ForMember(dest => dest.DateToBeIgnored, opt => opt.Ignore()) 
     .ForMember(dest=>dest.Settings, opt=>opt.UseDestinationValue()); 

    Mapper.CreateMap<AccountSettingViewModel, AccountSetting>() 
     .ForMember(dest=>dest.PropertyOne, opt=>opt.Ignore()) 
     .ForMember(dest => dest.PropertyTwo, opt => opt.MapFrom(a => a.PropertyTwo)); 

    Mapper.AssertConfigurationIsValid(); 

    AccountViewModel viewmodel = new AccountViewModel() 
    { 
     AccountId = 3, 
     DateToBeIgnored = DateTime.Now, 
     Settings = new AccountSettingViewModel() { PropertyTwo = "AccountSettingViewModelPropTwo" } 
    }; 

    Account account = new Account() 
    { 
     AccountId = 10, 
     DateToBeIgnored = DateTime.Now, 
     Settings = new AccountSetting() { PropertyOne = "AccountPropOne", PropertyTwo = "AccountPropTwo" } 
    }; 

    account = Mapper.Map<AccountViewModel, Account>(viewmodel, account); 

    Assert.IsNotNull(account); 

} 

result

+0

啊UseDestinationValue,這是偉大的,謝謝,我敢肯定,我試過,但我有一種感覺,我試圖設置它的屬性。謝謝! – Tim