简体   繁体   English

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

[英]Automapper many to many map fields from join

I am using Asp.net core, EF Core and Automapper. 我正在使用Asp.net核心,EF核心和Automapper。 I have a many to many (kind of, in EF Core its necessary to use a join table for this) and am having trouble mapping an additional field in the join table to the related collection inside the DTO I am returning. 我有很多(在EF Core中,必须为此使用联接表),并且在将联接表中的其他字段映射到我要返回的DTO内的相关集合时遇到了麻烦。

These are my entities (abbreviated): 这些是我的实体(缩写):

Event entity: 事件实体:

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

AppUser entity: AppUser实体:

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

EventAttendee entity: 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: EventToReturnDto:

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

AttendeeDto: AttendeeDto:

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

Automapper profile: 自动映射器配置文件:

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()));
    }    
}

Query: 查询:

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); 

This results in the following output (I at least get the user information) but I can't figure out how to get the 'DateJoined' property from the EventAttendee into the collection of Attendees returned and just get the default date as follows: 这将导致以下输出(至少我获得了用户信息),但我无法弄清楚如何从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"
        }
    ]
},

How would I go about creating the mapping to output the DateJoined property? 我将如何创建映射以输出DateJoined属性?

Thanks, James 谢谢,詹姆斯

First of all, you don't need both of these lines: 首先,您不需要这两行:

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

Because of your mapping instructions for the event, you are actually just mapping the AppUser but skip the EventAttendee completely. 由于事件的映射说明,您实际上只是在映射AppUser而完全跳过EventAttendee That is also why you are not able to get the DateJoined property. 这也是为什么您无法获得DateJoined属性的原因。

Instead, you should map the event just to the event DTO and then make a custom mapping for the EventAttendee : 相反,您应该将事件仅映射到事件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>();

So now, inside the event mapping, you are mapping the EventAttendee collection to an AttendeeDto collection, so you just need to set up the mapping for that. 因此,现在,在事件映射内部,您将EventAttendee集合映射到AttendeeDto集合,因此您只需要为此设置映射。 And within that, you can simply use navigation properties to access related entities. 在其中,您可以简单地使用导航属性来访问相关实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM