I think that the title already quite explains the problem. I have a source type:
public class Employee
{
public string Name { get; set; }
public string DateOfBirth { get; set; }
public string srcImage { get; set; }
public string Email { get; set; }
public string Role { get; set; }
}
and
public class EmployeeViewModel
{
public string Name { get; set; }
public string Surname { get; set; }
public string DateOfBirth { get; set; }
public string Email { get; set; }
public string Role { get; set; }
}
I want to use automapper to convert from EmployeeViewModel to Employee and the name of Employee is the concatenation of name and surname in EmployeeViewModel.
May you kindly explain me how to set the MapperConfiguration? Thanks!
Create a MapperConfiguration instance and initialize configuration via the constructor: var config = new MapperConfiguration(cfg => { cfg. CreateMap<Foo, Bar>(); cfg.
AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.
Try this:
Mapper.CreateMap<EmployeeViewModel, Employee>()
.ForMember(d => d.Name, d => d.MapFrom(x => string.Format("{0}{1}", x.Name, x.Surname)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With