Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper - RecognizePrefixes doesn't work

Tags:

c#

automapper

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, TpTasksEntity>(t);

It just does not work.

like image 646
user256034 Avatar asked Feb 01 '26 19:02

user256034


2 Answers

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);
like image 150
Darin Dimitrov Avatar answered Feb 04 '26 09:02

Darin Dimitrov


Use RecognizeDestinationPrefixes method.

like image 39
Martin Fabik Avatar answered Feb 04 '26 09:02

Martin Fabik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!