根据Valueinjecter,使用source和Target进行映射通常使用命名约定。但它在我的情况下不起作用,我如何管理导航属性的映射。
DTO
public class EmployeeDTO
{
public long EmployeeId { get; set; }
public long? LoginId { get; set; }
public string EmpNumber { get; set; }
public string FirstName { get; set; }
public string CompanyEmail { get; set; }
public string PersonalEmail { get; set; }
public AttendanceTimeSlotDTO AttendanceTimeSlot { get; set; }
}
public class AttendanceTimeSlotDTO
{
public int SlotId { get; set; }
public TimeSpan InTime { get; set; }
public TimeSpan OutTime { get; set; }
}
MYData提供商
public List<EmployeeDTO> GetActiveEmployees()
{
var employees = UnitOfWork.EmployeeRepository.Get(employee => employee.IsActive, null, "AttendanceTimeSlot").ToList();
//This work fine
var employeesDto = employees.Select(x => new EmployeeDTO().InjectFrom(x)).Cast<EmployeeDTO>().ToList();
employeesDto.InjectFrom(employees);
// Not Working
var result =employees.Select(e => new AttendanceTimeSlot().InjectFrom(e)).Cast<AttendanceTimeSlot>()
.Select(x => new EmployeeDTO().InjectFrom(x)).Cast<EmployeeDTO>().ToList();
}
MYEF
public long EmployeeId { get; set; }
public Nullable<long> LoginId { get; set; }
public string EmpNumber { get; set; }
public string FirstName { get; set; }
public virtual ICollection<Attendance> Attendances { get; set; }
public virtual ICollection<PermanentAddress> PermanentAddresses { get; set; }
public virtual ICollection<TemporaryAddress> TemporaryAddresses { get; set; }
public virtual AttendanceTimeSlot AttendanceTimeSlot { get; set; }
如何使用OMU.ValueInjector映射导航属性
答案 0 :(得分:1)
默认值ValueInjecter映射具有相同名称和类型的属性
行
employeesDto.InjectFrom(employees);
不需要,因为它没有做任何事情
这里:
employees.Select(e => new AttendanceTimeSlot().InjectFrom(e))
我没有在AttendanceTimeSlot和你的MYEF之间看到任何匹配的属性 所以MYEF没有int SlotId,TimeSpan InTime或TimeSpan OutTime,所以上面的行返回一个新创建的未触及AttendanceTimeSlot的集合
有关使用ValueInjecter和EntityFramework(代码优先)的示例,请查看此演示项目:http://prodinner.codeplex.com