我有BusinessLayer, DTO library,DataService, EntityModel(wher EDMX sits),DTO库指的是业务层和数据层。我正在尝试在数据层中实现automapper,想要将实体对象映射到 DTO 对象并从dataService库返回 DTO。
目前正在这样做
public class DataService
{
private MapperConfiguration config;
public DataService()
{
IMapper _Mapper = config.CreateMapper();
}
public List<Dto.StudentDto> Get()
{
using(var context = new DbContext().GetContext())
{
var studentList = context.Students.ToList();
config = new MapperConfiguration(cfg => {
cfg.CreateMap<Db.Student, Dto.StudentDto>();
});
var returnDto = Mapper.Map<List<Db.Student>, List<Dto.StudentDto>>(studentList);
return returnDto;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何将所有映射移至一个类,并且在调用数据服务器时自动映射器应自动初始化?
我有表老师和表学生,对于表'老师'中的每个记录,表'学生'中将有多个记录.这是我的模特课
public class Test
{
public int Id { get; set; }
public string Text { get; set; }
public List<StudentsDTO> Students{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的linq查询我试图获取记录
var st = (from tt in context.Teachers
join ss in context.Students
on tt.ID equals ss.teacherID
where tt.TypeID == 2
select new Test
{
Id = tt.ID,
Text = tt.Text,
Students= new List<StudentsDTO>()
{
new StudentsDTO()
{
Name= ss.Name,
Id= ss.StudentID
}
}.ToList()
}).ToList();
return st;
Run Code Online (Sandbox Code Playgroud)
我无法为教师表中的每条记录收集学生,该怎么办?