i have a class
public class Order
{
public string Name{ get; set; }
public Nullable<int> StatusId { get; set; }
public virtual Status Status { get; set; }
}
public class Status
{
public int Id { get; set; }
public string Name { get; set; }
}
public class OrderDto
{
public Nullable<int> StatusId { get; set; }
public string Name{ get; set; }
}
public class StatusDto
{
public int Id { get; set; }
public string Name { get; set; }
}
autoMapper:
cfg.CreateMap<Order, OrderDto>().ReverseMap();
cfg.CreateMap<Status, StatusDto>().ReverseMap();
In model classes I was able to do this:
name = order.status.Name
How can i achieve that when using autoMapper
Your OrderDto
does not have Status
, so you can not get Status
name from mapped object.
So either you will create new property in OrderDto
class StatusName
, and map it to status.name
of your model, or add to OrderDto
object StatusDto
field, like this:
public class OrderDto
{
public string Name{ get; set; }
public Nullable<int> StatusId { get; set; }
public StatusDto Status { get; set; }
}
Then AutoMapper will map models's Status
field to OrderDto
's Status
field.