I am new to automapper. need some help to map from ViewModel to Entity.
Here’s my user entity
public class User
{
public int Id { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime CreatedDate { get; set; }
public string DisplayName { get; set; }
}
here’s my ViewModel
public class UserViewModel
{
public string Email { get; set; }
public string Password { get; set; }
}
I create a map. its not working
CreateMap<UserLoginViewModel, User>()
.ForMember(dest=>dest.CreatedDate, DateTime.Now)
.ForMember(dest=>dest.DisplayName, "");
DisplayName and CreatedDate are required fields. since its not in the ViewModel, I will make DisplayName = “” and CreateDate = datetime.now.
I want to know if I can do it using the automapper, or I have to do it after the mapping.
please show me some sample code.
You mentioned that you have UserViewModel view model and User entity however your mapping configuration contains third type – UserLoginViewModel. Supposing that UserLoginViewModel is the same as UserViewModel, you should change you configuration as below.
AutoMapper wiki.