6
我試圖將ReferralContract.AssessmentId屬性映射到Referral.Assessment.Id 下面的代碼工作,但我相信有一個更乾淨的方式來做....請告訴我是這樣;-)AutoMapper如何映射來自ObjectId的嵌套對象
// Destination classes
public class Referral
{
public Referral()
{
Assessment = new Assessment();
}
public int Id { get; set; }
public Assessment Assessment { get; set; }
}
public class Assessment
{
public int Id { get; set; }
}
// Source Class
public class ReferralContract
{
public int Id { get; set; }
public int AssessmentId { get; set; }
}
我使用的Automapper映射
Mapper.CreateMap<ReferralContract, Referral>()
.ForMember(x => x.Assessment,
opt => opt.MapFrom(scr => new Assessment { Id = scr.AssessmentId }));
我有一個類似的情況與日期時間object.Date映射到/從一個object.SubClass.Date值...我得到一個'無法轉換對象類型'System.DateTime'鍵入'對象。子類錯誤,但一直在嘗試完全相同的映射初始化...任何建議? – beauXjames 2012-08-28 15:40:42