通过联接自动映射多对多地图字段

时间:2018-10-04 10:42:02

标签: c# asp.net-core entity-framework-core automapper

我正在使用Asp.net核心,EF核心和Automapper。我有很多(在EF Core中,这对于使用联接表是必要的),并且在将联接表中的其他字段映射到我要返回的DTO内的相关集合时遇到了麻烦。

这些是我的实体(缩写):

事件实体:

public class Event
{
   public int Id { get; set; }
   public string Title { get; set; }
   public ICollection<EventAttendee> Attendees { get; set; }
}

AppUser实体:

public class AppUser : IdentityUser<int>
{
    public string PhotoUrl { get; set; }
    public ICollection<EventAttendee> AttendingEvents { get; set; }
}

EventAttendee实体:

public class EventAttendee
{
    public int AppUserId { get; set; }
    public AppUser AppUser { get; set; }
    public int EventId { get; set; }
    public Event Event { get; set; }
    public DateTime DateJoined { get; set; }
}

EventToReturnDto:

public class EventToReturnDto
{
    public int Id { get; set; }
    public string Title { get; set; }
    public ICollection<AttendeeDto> Attendees { get; set; }
}

AttendeeDto:

public class AttendeeDto
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public DateTime DateJoined { get; set; }
}

自动映射器配置文件:

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<AppUser, AttendeeDto>();
        CreateMap<EventAttendee, AttendeeDto>();
        CreateMap<Event, EventToReturnDto>()
            .ForMember(dest => dest.Attendees,
                opt => opt.MapFrom(x => x.Attendees.Select(y => y.AppUser).ToList()));
    }    
}

查询:

var events = await _context.Events
    .Include(a => a.Attendees).ThenInclude(x => x.AppUser)
    .OrderBy(e => e.Date)
    .AsNoTracking()
    .ToListAsync(cancellationToken);

var eventsToReturn = _mapper.Map<List<EventToReturnDto>>(events); 

这将导致以下输出(我至少获得了用户信息),但是我无法弄清楚如何从EventAttendee中将'DateJoined'属性获取到返回的Attendees集合中,并且仅获取默认日期,如下所示:

{
    "id": 2,
    "title": "Event 2",
    "attendees": [
        {
            "id": 4,
            "userName": "Dave",
            "dateJoined": "0001-01-01T00:00:00"
        },
        {
            "id": 3,
            "userName": "Jane",
            "dateJoined": "0001-01-01T00:00:00"
        }
    ]
},

我将如何创建映射以输出DateJoined属性?

谢谢, 詹姆斯

1 个答案:

答案 0 :(得分:4)

首先,您不需要这两行:

CreateMap<AppUser, AttendeeDto>();
CreateMap<EventAttendee, AttendeeDto>();

由于该事件的映射说明,您实际上只是在映射AppUser,而完全跳过了EventAttendee。这就是为什么您无法获得DateJoined属性的原因。

相反,您应该将事件仅映射到事件DTO,然后为EventAttendee进行自定义映射:

CreateMap<EventAttendee, AttendeeDto>()
   .ForMember(d => d.Id, o => o.MapFrom(s => s.AppUserId))
   .ForMember(d => d.UserName, o => o.MapFrom(s => s.AppUser.UserName))
   .ForMember(d => d.DateJoined, o => o.MapFrom(s => s.DateJoined));
CreateMap<Event, EventToReturnDto>();

现在,在事件映射内部,您正在将EventAttendee集合映射到AttendeeDto集合,因此您只需要为此设置映射。在其中,您只需使用导航属性即可访问相关实体。