【问题标题】:EF Core with AutoMapper带有 AutoMapper 的 EF Core
【发布时间】:2020-11-27 05:30:51
【问题描述】:

我有一个包含属性和模型的 DTO,例如,学生可以有多个模块,一个模块可以与多个学生相关联。属性映射正常,但模型没有映射。

 public class GetStudentByIdMapping : Profile
{
    public GetStudentByIdMapping()
    {
        CreateMap<Student,StudentDetails>();
        CreateMap<Module, StudentDetails>()
            .ForPath(dest => dest.StudentModules.ModuleName, opt => opt.MapFrom(m => m.ModuleName))
            .ForPath(dest => dest.StudentModules.ModuleCode, opt => opt.MapFrom(m => m.ModuleCode))
            .ForPath(dest => dest.StudentModules.Description, opt => opt.MapFrom(m => m.Description))
            .ReverseMap();


    }
}


    public async Task<StudentDetails> GetStudent(int studentId)
    {
        var student = context.Student
                             .Where(s => s.StudentId == studentId)
                             .FirstOrDefault();

        var module = await context.Order
                           .Include(m => m.Module)
                           .Where(o => o.StudentId == studentId)
                           .Select(m => m.Module).ToListAsync();


        var studMap =  Mapper.Map<StudentDetails>(student);
        Mapper.Map<StudentDetails>(module);


        return studMap;
    }

这些是我想映射到 StudentDetails ViewModel 中的 Models 模型的 ViewModel

public class StudentDetails
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public StudentModule StudentModules { get; set; }

}
public class StudentModule
{
    public string ModuleName { get; set; }
    public string ModuleCode { get; set; }
    public string Description { get; set; }

}

这些是我的 EF Core 生成的实体

public partial class Student
{
    public Student()
    {
        Order = new HashSet<Order>();
    }

    public int StudentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public virtual ICollection<Order> Order { get; set; }
}

public partial class Module
{
    public Module()
    {
        Order = new HashSet<Order>();
    }

    public int ModuleId { get; set; }
    public int? LectureId { get; set; }
    public string ModuleName { get; set; }
    public string ModuleCode { get; set; }
    public string Description { get; set; }
    public string ModulePath { get; set; }

    public virtual Lecture Lecture { get; set; }
    public virtual ICollection<Order> Order { get; set; }
}

【问题讨论】:

  • 我们不清楚它们的结构和领域。建议您提供所有涉及的模型。
  • 我已经添加了 ViewModel 和我的实体
  • 我的回答是否帮助您解决了问题?如果解决了,请接受它作为答案,这将有助于其他有同样问题的人。如果没有,请告诉我们您的问题。

标签: automapper asp.net-core-webapi dto ef-core-2.1


【解决方案1】:

您只需将创建的目的地传递给第二个地图调用

映射的时候试试下面的代码:

 var studMap = Mapper.Map<Student,StudentDetails>(student);
 Mapper.Map<Module,StudentDetails>(module,studMap);

然后studMap 将接收所有映射的字段值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多