项目使用Automapper

时间:2017-12-21 15:34:50

标签: entity-framework automapper

我遇到了一个导致“类型出现在单个linq到实体查询中的两个结构上不兼容的初始化”错误的automapper问题。

我正在尝试ProjectTo<School>查询以检索(IQueryable)学校列表。

public IQueryable<School> GetSchools(string state = "", string county = "")
{                  
    return _adapter.GetSchools(state, county).ProjectTo<School>(_mapper.ConfigurationProvider);
}

问题是每个学校都包含Teacher列表,每个Teacher包含Class列表,每个Class包含Student列表每个Student都包含TestHistories列表,当查询由automapper ProjectTo<>生成时,它会尝试创建两个不同的Student初始化,其中一个初始化列表为{{1}一个没有:

TestHistories

}

.Lambda #Lambda10<System.Func`2[CoolTools4School.Data.Student,CoolTools4School.Library.Models.Student]>(CoolTools4School.Data.Student $dto)
{
.New CoolTools4School.Library.Models.Student(){
    StudentId = $dto.StudentId,
    Name = $dto.Name,
    ClassId = $dto.ClassId,
    Gender = $dto.Gender,
    TestHistories = .Call System.Linq.Enumerable.ToList(.Call System.Linq.Enumerable.Select(
            $dto.TestHistories,
            .Lambda #Lambda12<System.Func`2[CoolTools4School.Data.TestHistory,CoolTools4School.Library.Models.TestHistory]>))
}

}

我不想忽略配置中的测试历史或任何东西,因为我稍后会需要这些,但我不知道如何让查询创建两个新的.Lambda #Lambda11<System.Func`2[CoolTools4School.Data.Student,CoolTools4School.Library.Models.Student]>(CoolTools4School.Data.Student $dto) { .New CoolTools4School.Library.Models.Student(){ StudentId = $dto.StudentId, Name = $dto.Name, ClassId = $dto.ClassId, Gender = $dto.Gender } 查询相同在配置时不忽略测试历史记录的方式。

学生班看起来像这样:

Student

测试历史记录类如下所示:

public class Student
{
    public Guid StudentId { get; set; }
    public string Name { get; set; }
    public Guid? ClassId { get; set; }
    public string Gender { get; set; }

    public Class Class { get; set; }
    public List<TestHistory> TestHistories { get; set; } = new List<TestHistory>();
}

映射代码非常通用:

public class TestHistory
{
    public Guid TestHistoryId { get; set; }
    public DateTime DateTaken { get; set; }
    public TimeSpan ElaspedTime { get; set; }
    public int Score { get; set; }
    public Guid? StudentId { get; set; }
    public Guid? TestId { get; set; }

    public Student Student { get; set; }
    public Test Test { get; set; }
}
顺便说一下,以下代码有效,我只想找到一种方法来使用builder.Register(c => new MapperConfiguration(cfg => { cfg.CreateMap<Data.Teacher, Models.Teacher>().ReverseMap(); cfg.CreateMap<Data.Student, Models.Student>().ReverseMap(); cfg.CreateMap<Data.Test, Models.Test>().ReverseMap(); cfg.CreateMap<Data.TestHistory, Models.TestHistory>().ReverseMap(); cfg.CreateMap<Data.Question, Models.Question>().ReverseMap(); cfg.CreateMap<Data.School, Models.School>().ReverseMap(); cfg.CreateMap<Class, Models.Class>().ReverseMap(); //etc. })); ,而不必这样做:

ProjectTo<>
编辑:@rossbush指出,这也有效:

public IQueryable<School> GetSchools(string state = "", string county = "")
{
    var schools = _adapter.GetSchools(state, county);

    var list = new List<School>();
    foreach (var school in schools)
    {
        list.Add(_mapper.Map<School>(school));
    }
    return list.AsQueryable();    
}

0 个答案:

没有答案