简体   繁体   English

Automapper不会忽略嵌套属性

[英]Automapper not ignoring nested property

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. 当ViewModel ==> Entity direct时它工作正常但是当Automapper尝试更新嵌套对象时它失败了。

I have the following object structure: 我有以下对象结构:

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; }
}

With the mappings: 使用映射:

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();
}

When mapping [OtherAccountSettingViewModel --> AccountSetting], only the property "PropertyTwo" is assigned (and the original value for "PropertyOne" remains unchanged) -this is what I would expect. 映射[OtherAccountSettingViewModel - > AccountSetting]时,只分配属性“PropertyTwo”(“PropertyOne”的原始值保持不变) - 这就是我所期望的。

However when mapping [AccountViewModel --> Account] "DateToBeIgnored" is ignored as intended whereas Account.AccountSetting.PropertyTwo's previous value is replaced with "null". 但是,当映射[AccountViewModel - > Account]时,“DateToBeIgnored”将按预期被忽略,而Account.AccountSetting.PropertyTwo的先前值将替换为“null”。

Can anyone spot the error of my ways? 谁能发现我的方式错误?

Here is the solution: 这是解决方案:

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

}

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM