忽略使用 Automapper 映射一个属性 [英] Ignore mapping one property with Automapper

查看:28
本文介绍了忽略使用 Automapper 映射一个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Automapper,我有以下场景:类 OrderModel 有一个名为ProductName"的属性,该属性不在数据库中.因此,当我尝试使用以下方法进行映射时:

I'm using Automapper and I have the following scenario: Class OrderModel has a property called 'ProductName' that isn't in the database. So when I try to do the mapping with:

Mapper.CreateMap<OrderModel, Orders>(); 

它产生一个异常:

Project.ViewModels.OrderModel 上的以下 1 个属性未映射:'ProductName'

"The following 1 properties on Project.ViewModels.OrderModel are not mapped: 'ProductName'

我在 AutoMapper's Wiki for Projections 中阅读了相反的情况(额外的属性在目的地,而不是在源中,这实际上是我的情况)

I've read at AutoMapper's Wiki for Projections the opposite case (the extra attribute is on the destination, not in the source which is actually my case )

如何避免 automapper 对这个属性进行映射?

How can I avoid automapper to make the mapping of this property?

推荐答案

来自 Jimmy Bogard:CreateMap().ForMember(x => x.Blarg, opt => opt.Ignore());

From Jimmy Bogard: CreateMap<Foo, Bar>().ForMember(x => x.Blarg, opt => opt.Ignore());

它在 他博客上的评论之一.

更新(来自 杰米的评论 2019 年 1 月 4 日 11:11:)

UPDATE(from Jamie's comment Jan 4 '19 at 11:11:)

Ignore 已被 ForSourceMember 中的 DoNotValidate 替换:https://github.com/AutoMapper/AutoMapper/blob/master/docs/8.0-Upgrade-Guide.md

Ignore has been replaced with DoNotValidate in ForSourceMember: https://github.com/AutoMapper/AutoMapper/blob/master/docs/8.0-Upgrade-Guide.md

这篇关于忽略使用 Automapper 映射一个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆