自动映射器未忽略嵌套属性

4

我查看了各种类似的帖子,但无法发现我的错误。基本上,我有两个视图,更新“设置”对象的不同部分。视图模型包含两种属性中的一种,根据正在设置哪个属性,应忽略另一个属性。当ViewModel ==> Entity直接更新时,它运行良好,但是当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”。

是否有人能发现我的错误?

1个回答

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


Ah,使用UseDestinationValue,太好了,谢谢!我肯定尝试过,但我有一种感觉是我试图在属性上设置它。谢谢! - Tim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接