[英]AutoMapper: setup member name matching convention
I tried setting up a member name mapping convention so that the source members ending with a "Id" are mapped to destination members without Id. 我尝试设置成员名称映射约定,以便将以“ Id”结尾的源成员映射到不带ID的目标成员。 For example
例如
UserId -> User UserId->用户
How does one do this? 如何做到这一点? I tried using SourceMemberNameTransformer without success.
我尝试使用SourceMemberNameTransformer失败。 Also tried using RecognizePostfixes().
还尝试使用RecognizePostfixes()。
this.SourceMemberNameTransformer = s =>
{
return s.Replace("Id", string.Empty);
};
You can also use the "RecognizePostfixes" method: 您还可以使用“ RecognizePostfixes”方法:
this.RecognizePostfixes("Id");
The built-in transformer is this, just for future reference: 内置变压器就是这样,仅供以后参考:
s => Regex.Replace(s, "(?:^Get)?(.*)", "$1");
This should to work: 这应该工作:
this.SourceMemberNameTransformer = s => { if (s.EndsWith("Id")) return s.Substring(0, s.Length - 2); return s; };
You also can try achieve that with DestinationMemberNamingConvention
and regex. 您也可以尝试使用
DestinationMemberNamingConvention
和regex实现。
As of now this does not seem to work when setting it in the Profile
. 截至目前,在“
Profile
设置时,这似乎不起作用。 Neither SourceMemberNameTransformer
or RecognizePostfix
work in Profile
. SourceMemberNameTransformer
或RecognizePostfix
在Profile
不起作用。 However is specified in Automapper global configuration it works fine. 但是,在Automapper全局配置中指定它可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.