简体   繁体   English

自动映射器忽略自动展平

[英]Automapper Ignore Auto Flattening

I have a Entity in a legacy system which has the format 我在具有以下格式的旧版系统中有一个实体

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

In a ViewModel I have the following 在ViewModel中,我有以下内容

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

Mapping from The Entity to the View Model works fine, however when I try and map from the ViewModel to the Entity it dies. 从实体到视图模型的映射工作正常,但是,当我尝试从视图模型到实体的映射时,它就死了。

What it appears to be doing is trying to call a non existing property Duration.Type in the reverse mapping (ie it's trying to auto-flatten). 它似乎正在尝试在反向映射中调用不存在的属性Duration.Type(即,它试图自动展平)。 This results in the error Cannot map int32 to bool . 这导致错误Cannot map int32 to bool

Does anyone have any suggestions on how to disable auto-flattening in AutoMapper or manually set the fields which maps happen to using attributes. 是否有人对如何在AutoMapper中禁用自动拼合或手动设置使用属性映射的字段有任何建议。

To have it ignore the DurationType property when mapping from ViewModel to Entity add this to your mapping configuration: 要使其从ViewModel映射到Entity时忽略DurationType属性,请将其添加到映射配置中:

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

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

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