.net 如何使用 AutoMapper .ForMember?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6985000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:48:19  来源:igfitidea点击:

How to use AutoMapper .ForMember?

.netautomapper

提问by Nellius

I am trying to set up AutoMapper to convert from Entity to DTO. I know I'm supposed to be using .ForMember()after Mapper.CreateMap<Entity, DTO>()to set up custom mappings, but this doesn't seem to be an available method.

我正在尝试设置 AutoMapper 以从实体转换为 DTO。我知道我应该使用.ForMember()afterMapper.CreateMap<Entity, DTO>()来设置自定义映射,但这似乎不是一种可用的方法。

Edit for clarification: I am not looking for a link to the documentation, which I have read, or an explanation of the basic syntax. I am using the correct syntax as described in answers and the documentation, for example:

编辑澄清:我不是在寻找指向我已阅读的文档的链接,也不是在寻找基本语法的解释。我正在使用答案和文档中描述的正确语法,例如:

Mapper.CreateMap<EFAddress, Address>()
      .ForMember(dest => dest.Code, opt => opt.MapFrom(src => src.Name));

If I have an invalid type name within CreateMap<> I can see "ForMember" as a valid method, mousing over shows the method signature as I would normally expect. But as soon as I give it two valid types, ForMember says it cannot resolve the symbol, as if the method is not available.

如果我在 CreateMap<> 中有一个无效的类型名称,我可以看到“ForMember”是一个有效的方法,鼠标悬停会显示我通常期望的方法签名。但是一旦我给它两个有效类型,ForMember 就会说它无法解析符号,就好像该方法不可用一样。

Is there some kind of constraint on the generic classes which I am not meeting?

我没有遇到的泛型类是否有某种限制?

Thanks

谢谢

采纳答案by Nellius

In the end, I believe this turned out to be some kind of incompatibility with ReSharper.

最后,我相信这是与 ReSharper 的某种不兼容。

ReSharper seems to have caused Automapper code to display incorrectly, but work just fine (even though it displays red with error messages). Uninstalling ReSharper fixed this issue completely.

ReSharper 似乎导致 Automapper 代码显示不正确,但工作正常(即使它显示红色并带有错误消息)。卸载 ReSharper 完全解决了这个问题。

回答by Darin Dimitrov

Try the following syntax:

尝试以下语法:

Mapper
    .CreateMap<Entity, EntityDto>()
    .ForMember(
        dest => dest.SomeDestinationProperty,
        opt => opt.MapFrom(src => src.SomeSourceProperty)
    );

or if the source and destination properties have the same names simply:

或者如果源属性和目标属性具有相同的名称:

Mapper.CreateMap<Entity, EntityDto>();

Please checkout the relevant sections of the documentationfor more details and other mapping scenarios.

请查看文档的相关部分以获取更多详细信息和其他映射方案。

回答by stack72

a sample implementation would be as follows:

示例实现如下:

Mapper.CreateMap<Game, GameViewModel>()
  .ForMember(m => m.GameType, opt => opt.MapFrom(src => src.Type))

We need to map this property since the names of the properties of Game and GameViewModel are different - if they are the same and of the same type then it will not need a ForMember

我们需要映射此属性,因为 Game 和 GameViewModel 的属性名称不同 - 如果它们相同且类型相同,则不需要 ForMember

another use of the ForMember is to Ignore Mappings

ForMember 的另一个用途是忽略映射

Mapper.CreateMap<Game, GameViewModel>()
    .ForMember(dest => dest.Prize, opt => opt.Ignore());

回答by Mojtaba Nava

This use as well as:

这种用途以及:

  CreateMap<Azmoon, AzmoonViewModel>()
            .ForMember(d => d.CreatorUserName, m => m.MapFrom(s => 
 s.CreatedBy.UserName))
            .ForMember(d => d.LastModifierUserName, m => m.MapFrom(s => 
s.ModifiedBy.UserName)).IgnoreAllNonExisting();

回答by Richard Forrest

Are you doing it like this

你是这样做的吗

Mapper.CreateMap<SourceType,DestinationType>().ForMember(What ever mapping in here)

This pagehas some good examples

这个页面有一些很好的例子