0

I have the following POCO classes:

public class Employees
{
    public int EmployeeId
    {
        get;
        set;
    }
    public int EmpImageId
    {
        get;
        set;
    }

    public string EmployeePhotoUrl
    {
        get;
        set;
    }

    public string EmpAddress
    {
        get;
        set;
    }
}

public class Images
{
    public int ImageId
    {
        get;
        set;
    }

    public string ImageUrl
    {
        get;
        set;
    }

    public string ImgCode
    {
        get;
        set;
    }
}

public class EmployeeDTO
{
    public int EmployeeId
    {
        get;
        set;
    }
    public int EmpImageId
    {
        get;
        set;
    }

    public string EmployeePhotoUrl
    {
        get;
        set;
    }

    public string EmpAddress
    {
        get;
        set;
    }
}

While getting the list of Employees present in the system, for each Employee, the photourl (EmployeePhotoUrl) is fetched from the Images table using the EmpImageId property.

// Get the list of Employees
var employees = await _dbContext.Employees
                    .Select(ef => new EmployeeDTO
                    {
                        EmployeeId= ef.EmployeeId,                        
                        EmployeePhotoUrl = images.FirstOrDefault(im => im.ImageId.Equals(ef.EmpImageId)).EmployeePhotoUrl,
                        EmpAddress = ef.EmpAddress

                    }).Skip((pageNo - 1) * 100).Take(pageSize).ToListAsync();

I want to leverage Automapper in this case. The issue I see here is the assignment of EmployeePhotoUrl, since this property is fetched from another entity: Images

Can anyone help me to know how to leverage Automapper in this case.

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • You need to have the `Images` FK property in `Employee`. – Lucian Bargaoanu Nov 27 '19 at 12:24
  • Can you please help me with some code sample. Any help on this is much appreciated. I want to know how you are doing the mapping configuration. – santosh kumar patro Nov 27 '19 at 13:38
  • I was talking about the EF entity, not AM. – Lucian Bargaoanu Nov 27 '19 at 14:10
  • `Employees` already has an `EmployeePhotoUrl`, is there any reason that one can't be used? Also `Skip((pageNo - 1) * 100).Take(pageSize)` might want to change the `100` to `pageSize`. That said, this question already addresses how to map from multiple sources: https://stackoverflow.com/questions/21413273/automapper-convert-from-multiple-sources –  Nov 27 '19 at 15:21

0 Answers0