score:1

Accepted answer

First, I'm assuming your DTO is meant to contain public string SaleDepartmentDescription { get; set; } as your question refers to it but it isn't actually there.

If you are NOT using EF migrations (a fair assumption since otherwise you'd just add the foreign key!), then you can do this by adding keys in your Entities - the keys don't actually need to present in the database for EF to join on them, this just tells EF to pretend that they are. (If you are using EF migrations then this approach will not work, as it will want to add the keys to the DB.)

public class Consumable 
{
    public int ConsumableId { get; set; }
    public string Description { get; set; }
    public int SaleDepartmentId { get; set; }
    [ForeignKey("SaleDepartmentId")]
    public virtual SaleDepartment SaleDepartment { get; set; }
}

Assuming your DTO does contain the string property SaleDepartmentDescription then AutoMapper will handle this automatically, though you should use ProjectTo to make more efficient database queries:

var mappedDTOs = context.Consumable.ProjectTo<ConsumableDTO>().ToList();

Related Articles