【问题标题】:Automapper Ignore Auto FlatteningAutomapper 忽略自动展平
【发布时间】:2015-07-24 07:33:43
【问题描述】:

我在旧系统中有一个实体,其格式为

public Guid Id {get;set;}
public int Duration {get;set;}
public bool DurationType {get;set;}

在 ViewModel 中我有以下内容

public Guid Id {get; set;}
public int Duration {get;set;}

从实体映射到视图模型工作正常,但是当我尝试从视图模型映射到实体时,它会死掉。

它似乎在做的是试图在反向映射中调用一个不存在的属性 Duration.Type (即它试图自动展平)。这会导致错误Cannot map int32 to bool

是否有人对如何在 AutoMapper 中禁用自动展平或手动设置映射碰巧使用属性的字段有任何建议。

【问题讨论】:

    标签: c# automapper


    【解决方案1】:

    要让它在从ViewModel 映射到Entity 时忽略DurationType 属性,请将其添加到您的映射配置中:

    Mapper.CreateMap<ViewModel,Entity>()
          .ForMember(dest => dest.DurationType, options => options.Ignore());
    

    【讨论】:

    • 有没有办法通过属性来实现(只是看起来更干净一点)如果没有,不用担心。
    • 我不知道。我一直都是这样控制的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2017-12-29
    • 2018-11-07
    • 2021-11-27
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多