忽略使用Automapper映射一个属性

Msa*_*m85 263 c# model viewmodel automapper object-object-mapping

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

Mapper.CreateMap<OrderModel, Orders>(); 
Run Code Online (Sandbox Code Playgroud)

它会生成一个异常:

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

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

如何避免使用automapper来映射此属性?

sma*_*man 429

来自吉米博加德: CreateMap<Foo, Bar>().ForMember(x => x.Blarg, opt => opt.Ignore());

这是他博客上的一条评论.

  • 另外,`CreateMap <Foo,Bar>().ForSourceMember(x => x.Blarg,opt => opt.Ignore());`可能有用 (9认同)
  • 在ForSourceMember中,忽略已被DoNotValidate代替:https://github.com/AutoMapper/AutoMapper/blob/master/docs/8.0-Upgrade-Guide.md (5认同)
  • @Franva ForMember() 实际上是“ForDestinationMember()” (3认同)
  • @stackoverfloweth你的意思不是:`CreateMap &lt;SourceType,DestType&gt;(MemberList.Source).ForSourceMember(x =&gt; x.MySourceProperty,opt =&gt; opt.DoNotValidate())`吗? (2认同)
  • @Jamie @monty - 我开始更新此回复:您的评论,但看起来语法更改仅影响投影情况(其中需要忽略源属性)。OP 的请求是忽略目标属性,因此“Ignore()”仍然是正确的语法。这是因为“Ignore”的语法更改是在“ISourceMemberConfigurationExpression”接口上进行的,而不是在不相交的“IMemberConfigurationExpression”3“接口上进行的。 (2认同)

Ste*_*uts 226

我可能是一个完美主义者; 我不太喜欢ForMember(...,x => x.Ignore())语法.这是一件小事,但对我来说很重要.我写了这个扩展方法,使它更好一点:

public static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>(
    this IMappingExpression<TSource, TDestination> map,
    Expression<Func<TDestination, object>> selector)
{
    map.ForMember(selector, config => config.Ignore());
    return map;
}
Run Code Online (Sandbox Code Playgroud)

它可以像这样使用:

Mapper.CreateMap<JsonRecord, DatabaseRecord>()
        .Ignore(record => record.Field)
        .Ignore(record => record.AnotherField)
        .Ignore(record => record.Etc);
Run Code Online (Sandbox Code Playgroud)

您也可以重写它以使用它params,但我不喜欢带有大量lambda的方法的外观.

  • 我知道这超出了最初的问题,但我真的很喜欢这个答案,它干净,非常容易阅读和立即理解,并且易于重复使用 (6认同)
  • “ReverseMap”如何工作?ReverseMap().ForPath(... (2认同)
  • 我想我更愿意将其命名为“IgnoreMember()”,但是很棒的扩展! (2认同)

Ric*_*ard 79

你可以这样做:

conf.CreateMap<SourceType, DestinationType>()
   .ForSourceMember(x => x.SourceProperty, y => y.Ignore());
Run Code Online (Sandbox Code Playgroud)

  • @Redeemed1是的确如此. (3认同)
  • 对于问题中给出的情景,这应该是接受的答案.当前接受的答案忽略目标对象中属性的映射.这个问题是要求忽略源对象中的映射. (3认同)
  • 对于任何正在寻找扩展方法的人 public static IMappingExpression&lt;TSource, TDestination&gt; IgnoreSourceValidation&lt;TSource, TDestination&gt;( this IMappingExpression&lt;TSource, TDestination&gt; map, Expression&lt;Func&lt;TSource, object&gt;&gt; selector) { map.ForSourceMember(selector,配置=&gt; config.DoNotValidate()); 返回地图;} (2认同)
  • “ForSourceMember()”扩展上不存在“Ignore()”。正如@JasonDias 所说,它应该是“DoNotValidate()”。至少在最新版本的自动映射器中是这样。 (2认同)

Gui*_*ume 28

现在(AutoMapper 2.0)是一个IgnoreMap属性,我将使用它而不是流畅的语法,这是一个有点沉重的恕我直言.

  • ignore属性会通过您的应用程序泄漏自动映射器. (32认同)
  • AutoMapper是我不介意到处泄漏的一件事.;) (11认同)
  • 您始终可以考虑派生`IgnoreMapAttribute`. (4认同)
  • 这是忽略跨多个对象继承的基本属性的好方法。不必在每个映射配置中忽略它。 (2认同)

Sté*_*ane 28

对于任何尝试自动执行此操作的人,您可以使用该扩展方法忽略目标类型上的非现有属性:

public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
    var sourceType = typeof(TSource);
    var destinationType = typeof(TDestination);
    var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType)
        && x.DestinationType.Equals(destinationType));
    foreach (var property in existingMaps.GetUnmappedPropertyNames())
    {
        expression.ForMember(property, opt => opt.Ignore());
    }
    return expression;
}
Run Code Online (Sandbox Code Playgroud)

用作如下:

Mapper.CreateMap<SourceType, DestinationType>().IgnoreAllNonExisting();
Run Code Online (Sandbox Code Playgroud)

感谢Can Gencer提示:)

来源:http: //cangencer.wordpress.com/2011/06/08/auto-ignore-non-existing-properties-with-automapper/

  • 仅供参考:合并自http://stackoverflow.com/questions/4052579/automapper-how-to-ignore-property-in-source-item-that-does-not-exist-in-destinat (3认同)
  • 这在注入 IMapper 时不起作用。Mapper.GetAllTypeMaps 在最新版本的 AutoMapper 中不存在。此外,当我在 AutoMapper.Profile 中设置我的地图然后随后注入 IMapper 时,我收到此异常“Mapper 未初始化。使用适当的配置调用 Initialize。如果您尝试通过容器或其他方式使用映射器实例,请确保您没有对静态 Mapper.Map 方法的任何调用,如果您使用 ProjectTo 或 UseAsDataSource 扩展方法,请确保传入适当的 IConfigurationProvider 实例。” (2认同)
  • @Bassie使用`Mapper.Configuration.GetAllTypeMaps()`[源代码](https://github.com/AutoMapper/AutoMapper/issues/1252) (2认同)

Lor*_*sen 21

将视图模型映射回域模型时,简单地验证源成员列表而不是目标成员列表可以更加清晰

Mapper.CreateMap<OrderModel, Orders>(MemberList.Source); 
Run Code Online (Sandbox Code Playgroud)

现在Ignore(),每次我向域类添加属性时,我的映射验证都不会失败,需要另一个.

  • 这就是我所寻找的,当仅从更简单的DTO修改域对象属性的子集时非常有用. (6认同)
  • 这就是孩子们的答案,让这个新手不会混淆新手 (4认同)