代码之家  ›  专栏  ›  技术社区  ›  Mehmet Ince

自动映射多对多映射混淆

  •  0
  • Mehmet Ince  · 技术社区  · 9 年前

    到目前为止,我所做的是,

    映射配置 :

    CreateMap<Notification, NotificationViewModel>();
    CreateMap<User, UserViewModel>().ForMember(dest => dest.Notifications, map => map.MapFrom(src => src.UserNotification.Select(x => x.Notification)));
    

    类别:

    public class Notification : IEntityBase
    {
        public Notification()
        {
            this.UserNotification = new HashSet<UserNotification>();
        }
    
        public int Id { get; set; }
        public string Header { get; set; }
        public string Content { get; set; }
        public System.DateTime CreateTime { get; set; }
        public bool Status { get; set; }
    
        public virtual ICollection<UserNotification> UserNotification { get; set; }
    }
    

    public class User : IEntityBase
    {
        public User()
        {
            this.UserNotification = new HashSet<UserNotification>();
        }
    
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public bool Status { get; set; }
    
        public virtual ICollection<UserNotification> UserNotification { get; set; }
    }
    

    用户通知

    public class UserNotification : IEntityBase
    {
        public int Id { get; set; }
        public int UserId { get; set; }
        public int NotificationId { get; set; }
        public bool HasRead { get; set; }
    
        public virtual Notification Notification { get; set; }
        public virtual User User { get; set; }
    }
    

    班

    public class UserViewModel : IValidatableObject
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }
        public string PhoneNumber { get; set; }
        public bool Status { get; set; }
    
        public IList<NotificationViewModel> Notifications { get; set; }
    }
    

    班

    public class NotificationViewModel
    {
        public int Id { get; set; }
        public string Header { get; set; }
        public string Content { get; set; }
        public System.DateTime CreateTime { get; set; }
        public bool Status { get; set; }
        public bool HasRead { get; set; } // this is the difference
    }
    
    1 回复  |  直到 9 年前
        1
  •  2
  •   grek40    9 年前

    为了修复 HasRead ,也许你可以利用 AfterMap(Action<TSource, TDestination> afterFunction)

    CreateMap<User, UserViewModel>()
        .ForMember(dest => dest.Notifications, map => map.MapFrom(src => src.UserNotification.Select(x => x.Notification)))
        .AfterMap((src, dest) =>
        {
            foreach (var notificationVM in dest.Notifications)
            {
                notificationVM.HasRead = src.UserNotification.Where(x => x.NotificationId == notificationVM.Id).Select(x => x.HasRead).FirstOrDefault();
            }
        });
    
    推荐文章