简体   繁体   English

使用 AutoMapper Map function 映射对象时,将源 object 的属性保留到目标属性

[英]Keep property of source object to destination property when mapping objects using AutoMapper Map function

I want to map most properties of the current object ( this a FinancialBase instance) to another object (the 'destination' object, the schedule , an instance of a Schedule class). I want to map most properties of the current object ( this a FinancialBase instance) to another object (the 'destination' object, the schedule , an instance of a Schedule class). However, I need to keep a small set of the destination's properties.但是,我需要保留一小部分目的地的属性。

I've got it working with a 'hack' where I capture the values explicitly then use these in the AfterMap function.我已经让它与“hack”一起工作,我在其中明确捕获值,然后在AfterMap function 中使用这些值。 See example code.请参阅示例代码。

var id = schedule.Id;
var parentId = schedule.ParentId;
var scheduleNo = schedule.ScheduleNo;
var schName = schedule.SchName;

var config = new MapperConfiguration(
    cfg => cfg.CreateMap<FinancialBase, Schedule>()
    .ForMember(d => d.Id, opt => opt.Ignore())
    .ForMember(d => d.ParentId, opt => opt.Ignore())
    .ForMember(d => d.ScheduleNo, opt => opt.Ignore())
    .ForMember(d => d.SchName, opt => opt.Ignore())
    .AfterMap((s, d) => d.Id = id)
    .AfterMap((s, d) => d.ParentId = parentId)
    .AfterMap((s, d) => d.ScheduleNo = scheduleNo)
    .AfterMap((s, d) => d.SchName = schName));
var mapper = config.CreateMapper();
schedule = mapper.Map<Schedule>(this);

I would prefer not to use the first four lines of my example but instead have them included using a conventional AutoMapper lambda expression.我不想使用示例的前四行,而是使用传统的 AutoMapper lambda 表达式将它们包含在内。 Possible?可能的?

I'd just use mapping to an existing object:我只是使用映射到现有的 object:

var existingSchedule = new Schedule()
{
    Id = 12,
    ParentId = 34,
    ScheduleNo = 56,
    SchName = "Foo",
};

var schedule = mapper.Map(this, existingSchedule);

And in the configuration leave the Ignore() lines but remove those with AfterMap() as they are no longer needed:并在配置中保留Ignore()行,但使用AfterMap()删除那些不再需要的行:

var config = new MapperConfiguration(
    cfg => cfg.CreateMap<FinancialBase, Schedule>()
        .ForMember(d => d.Id, opt => opt.Ignore())
        .ForMember(d => d.ParentId, opt => opt.Ignore())
        .ForMember(d => d.ScheduleNo, opt => opt.Ignore())
        .ForMember(d => d.SchName, opt => opt.Ignore()));

暂无
暂无

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

相关问题 自动映射器,将Source映射到Destination的属性 - Automapper, map Source to property of Destination 使用 Automapper 到 map 从对象列表到单个 object 使用来自源的属性值 - Use Automapper to map from a list of objects to a single object using property values from source 自动映射器:将类型X的属性从源对象映射到目标对象,并将等值属性映射为类型X - Automapper: Map property of type X from source object to destination object with equivlanet properties to type X 自动映射 - 当源对象具有值的属性时,不映射 - Automapper - Don't map when a source object has a property with a value AutoMapper - 当源属性不可用时,将目标属性设置为 null - AutoMapper - set a destination property to null when source property is not available A类型的自动映射器将源属性映射到目标列表 - Automapper map source property of type A to destination List<B> Automapper 仅在映射对象列表时覆盖不在源中的目标值 - Automapper overrinding destination values that are not in source ONLY when mapping lists of objects 如何在Automapper映射中保留源属性的原始值? - How to keep original value of a source property in Automapper mapping? 使用 AutoMapper 进行映射时如何忽略 Destination 集合类型属性? - How do I ignore Destination collection type property when mapping using AutoMapper? 自定义将属性从目标映射到源并反向映射 - Customize mapping a property from destination to source and reverse map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM