2016-11-24 91 views
0
[DataContract] 
    public class SupplierView : BaseView 
    { 
     [DataMember] 
     Public int SupplierId { get; set; } 
     [DataMember] 
     Public ApprovedSupplierView approvedSupplier { get; set; } 
     [DataMember] 
     Public AdHocSupplierView adhocsupplier { get; set; } 
     //other fields... 
    } 
    [DataContract] 
    public class ContextSupplierView : SupplierView //Working 
    { 
     [DataMember] 
     Public int SupplierId { get; set; } 
     //New fields added 
     [DataMember] 
     Public ContextApprovedSupplierView contextApprovedSupplier { get; set; } 
     [DataMember] 
     Public ContextAdHocSupplierView contextAdhocsupplier { get; set; } 
     //other fields... 
    } 
    public class ApprovedSupplierView : BaseView 
    { 
     [DataMember] 
     Public string Name { get; set; } 
     [DataMember] 
     Public string Phone { get; set; } 




    [DataContract] 
    public class ContextSupplierView : SupplierView //Not working 
    { 
     [DataMember] 
     Public int SupplierId { get; set; } 
     [DataMember] 
     Public new ContextApprovedSupplierView approvedSupplier { get; set; } 
     [DataMember] 
     Public new ContextAdHocSupplierView adhocsupplier { get; set; } 
     //other fields... 
    } 

    [DataContract] 
    public class ContextApprovedSupplierView : ApprovedSupplierView 
    { 
     [DataMember] 
     Public string ContextDescription { get; set; } 

    SupplierView supplierObject = new SupplierView(Linq populates this correctly); 
    Mapper.CreateMap<SupplierView, ContextSupplierView>(); 
    claimContext.Supplier = Mapper.Map<ContextSupplierView>(supplierObject); 

我有一個已經得到供應商和嵌套類型從數據庫中的一個項目,但我該如何使用automapper一切複製到其中有ContextApprovedSupplier approvedSupplier和ContextAdHocSupplier一個ContextSupplier adHocSupplier但是,這給出:automapper:映射覆雜的源類tocomplex派生類

缺少類型映射配置或不支持的映射。

映射類型:ApprovedSupplierView - > ContextApprovedSupplierView TotalSystemsPlc.Bluescape.Claims.DataContracts.Views.ProjectLoadViews.ApprovedSupplierView - > TotalSystemsPlc.Bluescape.Claims.DataContracts.Views.ContextViews.ContextApprovedSupplierView

目標路徑: ContextSupplierView .ApprovedSupplier.ApprovedSupplier

來源值: TotalSystemsPlc.Bluescape.Claims.DataContracts.Views.ProjectLoadViews.ApprovedSupplierView

如果我創建新字段contextApprovedSupplier和contextAdhocsupplier,它會自動填充ApprovedSupplier和Adhocsupplier,它可以單獨映射,但我寧願在一個命令中完成所有操作。 任何幫助將得到很好的收到,因爲我一直在嘗試幾個小時來做​​到這一點!

編輯:道歉,請現在找到正確的層次!

+0

什麼是'SupplierView'和'ContextSupplierView'? –

+0

Shoudn't'ContextSupplier'是從'Supplier'派生的嗎?如果沒有,則提供聲明隱藏屬性的'Base'類'approvedSupplier'' adhocsupplier' – Rafal

+1

您可以嘗試添加映射:'Mapper.CreateMap ()'但在這種情況下您的視圖不應該返回'ContextApprovedSupplierView '? – Rafal

回答

0

enter image description here

@Rafal添加映射在一起真的不錯,非常感謝你!

Mapper.CreateMap<SupplierView, ContextSupplierView>(); 
Mapper.CreateMap<ApprovedSupplierView, ContextApprovedSupplierView>(); 
Mapper.CreateMap<AdHocSupplierView, ContextAdHocSupplierView>(); 

我也不得不增加一個默認的構造函數發出這指的是缺少投的錯誤!我期待着更多地使用Automapper。