2015-09-12 89 views
0

我有三個業務對象:AutoMapper繼承映射不​​工作

​​

PayCaddyEntity基本上只是提供了一個共同Id財產,作爲一個「標誌」我可以在通用的限制使用。

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

在AutoMapper 4.0中改變了繼承 - 爲了修復一些場景,繼承鏈在配置時計算。在配置後撥打Mapper.Configuration.Seal(),或者更好的是,使用Mapper.Initialize()而不是Mapper.CreateMap