简体   繁体   English

Automapper按名称将UserId属性映射为静态值

[英]Automapper Map UserId Property By Name to static value

I use automapper to map models between each user. 我使用自动映射器在每个用户之间映射模型。 I can also do this to map a property of a specific type to a userId 我也可以这样做以将特定类型的属性映射到userId

  cfg.CreateMap<Model, Command>()
                .ForMember(dest => dest.UserId, opt => ClaimsPrincipal.Current.UserId))

But I can't find a way to make this to some kind of convention so that everytime models are mapped and there is a Property "UserId" it should be set to ClaimsPrincipal.Current.UserId. 但是我找不到一种使这种习惯成为某种惯例的方法,以便每次映射模型并有一个属性“ UserId”时,应将其设置为ClaimsPrincipal.Current.UserId。

Is this now working ? 现在可以了吗?

Any advice? 有什么建议吗?

Have you tried the MapTo Attribute? 您是否尝试过MapTo属性?

public class Foo
{
    [MapTo("SourceOfBar")]
    public int Bar { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM