Automapper 忽略自动展平

Automapper Ignore Auto Flattening

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

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 中禁用自动展平或手动设置地图恰好使用属性的字段有任何建议。

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

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