2015-02-09 184 views
2

帕特里克,感謝您對正確問題的建議!Automapper多對多映射

編輯1:

我有三個表多對多的關係。就像這樣: EF data model

GoodEntity:

public partial class GoodEntity 
{ 
    public GoodEntity() 
    { 
     this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>(); 
    } 

    public int id { get; set; } 
    public string name { get; set; } 
    public string description { get; set; } 
    public decimal cost { get; set; } 
    public Nullable<decimal> price { get; set; } 

    public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; } 
} 

ProviderEntity:

public partial class ProviderEntity 
{ 
    public ProviderEntity() 
    { 
     this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>(); 
    } 

    public int id { get; set; } 
    public string name { get; set; } 
    public string description { get; set; } 
    public string address { get; set; } 
    public string phone { get; set; } 
    public string email { get; set; } 
    public string url { get; set; } 
    public Nullable<int> rating { get; set; } 

    public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; } 
} 

實體許多一對多的關係:

public partial class GoodAndProviderEntity 
{ 
    public int id { get; set; } 
    public int good_id { get; set; } 
    public int provider_id { get; set; } 

    public virtual GoodEntity Goods { get; set; } 
    public virtual ProviderEntity Providers { get; set; } 
} 

GoodDTO:

public class GoodDTO 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string description { get; set; } 
    public decimal cost { get; set; } 
    public decimal? price { get; set; } 

    public IList<ProviderDTO> providers { get; set; } 
} 

ProviderDTO:

public class ProviderDTO 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string description { get; set; } 
    public string address { get; set; } 
    public string phone { get; set; } 
    public string email { get; set; } 
    public string url { get; set; } 
    public int? rating { get; set; } 
} 

這是創作的地圖代碼:

Mapper.CreateMap<ProviderDTO, ProviderEntity>(); 
Mapper.CreateMap<ProviderEntity, ProviderDTO>(); 

Mapper.CreateMap<GoodEntity, GoodDTO>() 
     .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders)); 
Mapper.CreateMap<GoodAndProviderEntity, ProviderDTO>(); 

和它的作品的一半。 Automapper被完全映射爲「商品」,並被創建爲所有商品供應商的列表。但automapper不會填充提供程序。 enter image description here

如果我使用Mapper.AssertConfigurationIsValid(),則:

未映射成員被發現。查看下面的類型和成員。添加自定義映射表達式,忽略,添加自定義解析器或修改源/目標類型============================== ========================= ProviderDTO - > ProviderEntity(目標成員列表)Core.DTO.ProviderDTO - > DAL.EF.Entities.ProviderEntity(Destination會員列表)未映射的屬性:GoodsAndProviders =========================================== =================== GoodAndProviderEntity - > ProviderDTO(目標成員列表)DAL.EF.Entities.GoodAndProviderEntity - > Core.DTO.ProviderDTO(目標成員列表)

如何創建多對多關係的映射?

問候,安東

+0

會發生什麼公司變量上的'F12'?這應該告訴你它在哪裏定義。 – 2015-02-09 16:08:57

+0

安德魯,我不能這樣做,因爲我沒有回答源代碼。 – Hellaren 2015-02-09 16:15:18

+0

我建議您進行一些編輯以幫助您獲得答案:1)刪除問題的開頭,在那裏引用另一個SO問題的答案。沒有看到所有的代碼,我們不能告訴你'公司'來自哪裏。2)請提供您的源類和目標類的完整定義(不只是它們如何相關的圖形)。 3)顯示你如何映射,採樣數據和映射失敗的地方。 4)確保你調用Mapper.AssertConfigurationIsValid()來確保你的映射配置正確。 – PatrickSteele 2015-02-10 12:39:29

回答

5

根據您目前的代碼,你想映射到GoodAndProviderEntity ProviderDTO。

Mapper.CreateMap<GoodEntity, GoodDTO>() 
    .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders)); 

你想要做什麼,是ProviderEntity映射到ProviderDTO,因此,所有你需要做的就是選擇GoodsAndProviders的提供商列表:當您使用`Shift`

Mapper.CreateMap<GoodEntity, GoodDTO>() 
     .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders.Select(y => y.Providers).ToList()));