1

Automapper v4.0 は、メソッド内で使用するのが非常に簡単でした。誰かがこれを v5.0 用に書き直すのを手伝ってくれませんか (具体的には Mapper コード):

    public IEnumerable<NotificationDto> GetNewNotifications()
    {
        var userId = User.Identity.GetUserId();
        var notifications = _context.UserNotifications
            .Where(un => un.UserId == userId && !un.IsRead)
            .Select(un => un.Notification)
            .Include(n => n.Gig.Artist)
            .ToList();

        Mapper.CreateMap<ApplicationUser, UserDto>();
        Mapper.CreateMap<Gig, GigDto>();
        Mapper.CreateMap<Notification, NotificationDto>();

        return notifications.Select(Mapper.Map<Notification, NotificationDto>);
    }

更新: EF Core は、AutoMapper がマッピングしているものを投影していないようです:

return notifications.Select(Mapper.Map<Notification, NotificationDto>);

しかし、次のコードを使用して Postman で結果を取得します。

        return notifications.Select(n => new NotificationDto()
        {
            DateTime = n.DateTime,
            Gig = new GigDto()
            {
                Artist = new UserDto()
                {
                    Id = n.Gig.Artist.Id,
                    Name = n.Gig.Artist.Name

                },
                DateTime = n.Gig.DateTime,
                Id = n.Gig.Id,
                IsCancelled = n.Gig.IsCancelled,
                Venue = n.Gig.Venue
            },
            OriginalVenue = n.OriginalVenue,
            OriginalDateTime = n.OriginalDateTime,
            Type = n.Type
        });
4

1 に答える 1