如何使用AutoMapper .ForMember?如何使用、AutoMapper、ForMember

2023-09-03 09:53:02 作者:得不到的永远在骚动

我试图建立AutoMapper转换,从实体到DTO。我知道我应该使用 .ForMember() Mapper.CreateMap<实体,DTO>()建立自定义映射,但这似乎不是一个有效的方法。

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.

编辑澄清: 我不是在寻找一个链接的文档,我曾经读过的,或者基本语法的解释。我使用的是正确的语法中的答案和文档描述,例如:

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));

如果我有内CreateMap℃的无效的类型名称;>我可以看到ForMember作为一种有效的方法,鼠标悬停显示,因为我通常会想到的方法签名。但只要我给它两个有效的类型,ForMember说,它无法解析符号,因为如果该方法不可用。

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.

是否有某种约束的泛型类而我没有达到?

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

感谢

推荐答案

在最后,我相信这竟然是一些不兼容的情况与ReSharper的。

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

ReSharper的似乎造成Automapper code显示不正常,但只是正常工作(即使它显示红色的错误消息)。卸载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.

 
精彩推荐
图片推荐