Take this database for example
Employee
- id - int (PK)
- name - varchar
Salary
- id - int (PK)
- employee_id - int (FK)
- amount - float
Entity Framework will generate models similar to these:
public partial class Employee
{
public Employee()
{
this.Salaries = new HashSet<Salary>();
}
public int id { get; set; }
public string name { get; set; }
}
public partial class Salary
{
public int id { get; set; }
public int employee_id { get; set; }
public float amount { get; set; }
public Employee employee { get; set; }
}
The Emplyee references a list of his Salaries while each Salary points to which employee he owns. This results in a circular reference problem.
I follow a repository pattern and use AutoMapper to transfer Employee to EmployeeDTO and Salary to SalaryDTO. I want those DTOs to keep infomation of it's children relationships. However, I dont want to do this recursively. I COULD do something like.
public partial class EmployeeDTO
{
public EmployeeDTO()
{
this.Salaries = new HashSet<SalaryChildDTO>();
}
public int id { get; set; }
public string name { get; set; }
}
public partial class SalaryChildDTO
{
public int id { get; set; }
public float amount { get; set; }
}
But this would become a maintenance nightmare.
How can I tell AutoMapper to only map a single decendant, or acheive a similar goal?