AutoMapper继承了映射无法正常工作

时间:2015-09-12 05:26:54

标签: .net asp.net-mvc mapping automapper

我有三个业务对象:

public abstract class PayCaddyEntity : IPayCaddyEntity
{
    protected PayCaddyEntity()
    {
        if (IsPopulated && string.IsNullOrWhiteSpace(Id))
        {
            Id = Guid.NewGuid().ToString();
        }
    }
    ...
}

PayCaddyEntity基本上只提供一个共同的Id属性,并充当“{1}”标记'我可以在通用约束中使用。

public abstract class PayCaddyMessage : PayCaddyEntity
{
    public string Subject { get; set; }
    public ApplicationUser Sender { get; set; }
    public ApplicationUser Receiver { get; set; }
    public DateTime? TimeSent { get; set; }
    public DateTime? TimeReceived { get; set; }
}

public class BetPaymentOffer: PayCaddyMessage
{        
    public string Description { get; set; }
    public int Amount { get; set; }
    public bool PaymentAccepted { get; set; }
}

我尝试将它们映射到公共视图模型,如下所示:

Mapper.CreateMap<PayCaddyMessage, BetPaymentOfferViewModel>()
    .Include<BetPaymentOffer, BetPaymentOfferViewModel>();
Mapper.CreateMap<BetPaymentOffer, BetPaymentOfferViewModel>();

var offer = new BetPaymentOffer { Sender = GetCurrentUser() };
var model = Mapper.Map<BetPaymentOfferViewModel>(offer);

未映射派生类上设置的基类属性。在model中,Sender为空。我做错了什么?

1 个答案:

答案 0 :(得分:0)

AutoMapper 4.0中的继承已更改 - 为了修复多个方案,继承链在配置时计算。配置后调用Mapper.Configuration.Seal(),甚至更好,使用Mapper.Initialize()代替Mapper.CreateMap