繁体   English   中英

Automapper多对多映射混乱

[英]Automapper many to many mapping confusion

我有很多对许多关系表,例如“用户与通知和用户通知”及其实体,还可以查看模型。

ViewModel和Entity类之间只有一个区别。 HasRead属性位于NotificationViewModel内部。 如何映射该实体以查看模型? 对于HasRead属性,我无法实现此目的。

我到目前为止所做的是

映射配置

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

UserNotification类:

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

UserViewModel

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

NotificationViewModel

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
}

为了修复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();
        }
    });

暂无
暂无

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

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