开发者

Automapper - RecognizePrefixes doesn't work

开发者 https://www.devze.com 2023-03-05 20:57 出处:网络
I have need to map PriorityId -> TcTaskPriorityId Mapper.Configuration.RecognizePrefixes(\"TcTask\");

I have need to map PriorityId -> TcTaskPriorityId

 Mapper.Configuration.RecognizePrefixes("TcTask");
 Mapper.CreateMap<Task, TpTasksEntity>();

 Task t = new Task{PriorityId = 1};          
 var te = Mapper.Map<Task, TpTasksEnt开发者_运维技巧ity>(t);

It just does not work.


The RecognizePrefixes works for source object prefixes, i.e.:

Mapper.Configuration.RecognizePrefixes("TcTask");
Mapper.CreateMap<Task, TpTasksEntity>();

Task t = new Task { TcTaskPriorityId = 1 };
var te = Mapper.Map<Task, TpTasksEntity>(t);

For your scenario you could write a custom naming convention:

Mapper.Configuration.SourceMemberNameTransformer = s => "TcTask" + s;
Mapper.CreateMap<Task, TpTasksEntity>();

Task t = new Task { PriorityId = 1 };
var te = Mapper.Map<Task, TpTasksEntity>(t);


Use RecognizeDestinationPrefixes method.


Can you try:

Mapper.Initialize(cfg => {
    cfg.RecognizePrefixes("TcTask");
    cfg.CreateMap<Task, TpTasksEntity>();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消