Automapper没有忽视嵌套属性 [英] Automapper not ignoring nested property

查看:150
本文介绍了Automapper没有忽视嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过各种类似的职位的外观,但不能当场我与此一方式错误。基本上我有一个设置对象的两个视图其中更新不同的部分。视图模型包含两个属性中的一个,并根据在其上被设置,应忽略其他。它正常工作时,它的视图模型==>实体直接但当Automapper尝试更新嵌套对象失败

I've had a look through the various similar posts but can't spot the error of my ways with this one. Basically I have two views which update different parts of a "Settings" object. The view models contain one of two properties and depending on which is being set, should ignore the other. It works fine when it's ViewModel ==> Entity direct but when Automapper tries to update the nested object it fails.

我有以下对象结构:

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的原始值保持不变) - 这是我所期望的。

When mapping [OtherAccountSettingViewModel --> AccountSetting], only the property "PropertyTwo" is assigned (and the original value for "PropertyOne" remains unchanged) -this is what I would expect.

不过映射[AccountViewModel - >帐户]当DateToBeIgnored是被有意忽略,而Account.AccountSetting.PropertyTwo的前值被替换为空。

However when mapping [AccountViewModel --> Account] "DateToBeIgnored" is ignored as intended whereas Account.AccountSetting.PropertyTwo's previous value is replaced with "null".

任何人都可以发现我的方式错误?

Can anyone spot the error of my ways?

推荐答案

下面是解决方案:

[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);

}



这篇关于Automapper没有忽视嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆