We're using AutoMapper to do our dababase<=>viewmodel conversions.
In our database, we have a record that contains a customerid. Our viewmodel does not.
When we map from the database to the viewmodel, it's easy enough to ignore the customerid. But when we map from the viewmodel to the database, how can we set the customerid?
We know the value we want it to be set to, at the time we're doing the mapping, but how do we get it into the map configuration?
public class DbUser
{
public string customerid { get; set; }
public string userid { get; set; }
public string userinfo { get; set; }
}
public class UserModel
{
public string userid { get; set; }
public string userinfo { get; set; }
}
// mapping DbUser to UserModel is trivial
Mapper.CreatedMap<DbUser, UserModel>();
// mapping UserModel to DbUser, how do we set customerid?
Mapper.CreatedMap<UserModel, DbUser>()
.ForMember(u => u.customerid, opt => opt.Ignore();
// One way is simple
var dbUser = this.dbContext.DbUsers.FirstOrDefault();
var userModel = Mapper.Map<UserModel>(dbUser);
// The other way I don't like
var userModel = getUserModel();
var dbUser = Mapper.Map<DbUser>(userModel);
dbUser.customerid = "TheCustomerid";
Is there a way of setting the customerid within the mapping, where the value is provided at the time the mapping is done, rather than setting the value afterwards?