I have the following classes:
public partial class ScheduledDeduction
{
public int ID { get; set; }
public int DriverId { get; set; }
public string Description { get; set; }
public DateTime DateTime { get; set; }
public decimal Amount { get; set; }
public virtual Driver Driver { get; set; }
}
public partial class Driver
{
public int ID { get; set; }
public int CompanyId { get; set; }
public string Name { get; set; }
and View Model class:
public abstract class ScheduledDeductionDetailVM
{
public int ID { get; set; }
[Display(Name = "Driver Name")]
public string DriverName { get; set; }
public string Description { get; set; }
[Display(Name = "Date")]
[DisplayFormat(DataFormatString = "{0:d}")]
public System.DateTime DateTime { get; set; }
[Display(Name = "Amount")]
[DisplayFormat(DataFormatString = "{0:c}")]
public decimal Amount { get; set; }
}
I have the following rule of Automapper:
CreateMap<Infrastructure.Asset.ScheduledDeduction, ViewModels.ScheduledDeductionDetailVM>();
and try to use:
ScheduledDeduction scheduledDeduction = db.ScheduledDeductions.Find(id);
ScheduledDeductionDetailVM model = mapper.Map<ScheduledDeductionDetailVM>(scheduledDeduction);
and it works! Why? ScheduledDeductionDetailVM has property DriverName , which can be got from ScheduledDeduction.Driver.Name and it never described at all. But it's mapped correctly...