我有三个业务对象:
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
为空。我做错了什么?
答案 0 :(得分:0)
AutoMapper 4.0中的继承已更改 - 为了修复多个方案,继承链在配置时计算。配置后调用Mapper.Configuration.Seal()
,甚至更好,使用Mapper.Initialize()
代替Mapper.CreateMap
。