2014-08-28 68 views
0
public class Technology : EntityBase 
{ 
    [NotNullNotEmpty] 
    [Length(ColumnMetadata.LongTextLength)] 
    public virtual string Name { get; set; } 

    public virtual IList<TechnologyTechCategories> TechCategories { get; set; } 
} 

public class TechnologyTechCategories : EntityBase 
{ 
    [NotNull] 
    public virtual Technology Technology { get; set; } 
    [NotNull] 
    public virtual TechCategory TechCategory { get; set; } 
} 

public class TechCategory : ReferenceBase 
{ 
} 

public class TechDetailModel 
{ 
    public virtual int Id { get; set; } 

    [Required] 
    public virtual string Name { get; set; } 

    [DisplayName("Tech Categories")] 
    [Required] 
    public int[] TechCategories { get; set; } 

    public virtual IEnumerable<SelectListItem> Categories { get; set; } 
} 

這些是我上面的類。所以在綁定到控制器時,我會忽略多選下拉列表的類別集合。但我不能讓自動映射器初始化IList int [] TechCategories。有人可以幫助如何將貼圖放在一起?nhibernate + Automapper和多對多的關係

Mapper.CreateMap<Technology, TechDetailModel>() 
       .ForMember(c => c.Categories, option => option.Ignore()) 
       .ReverseMap(); 

回答

0

你正在尋找自動地圖複雜類型簡單類型這裏即IList的=> INT []

我假設EntityBase有一些用於身份屬性的例如ID

所以下面的映射可能會爲你工作:

  Mapper.CreateMap<Technology, TechDetailModel>() 
      .ForMember(c => c.Categories, option => option.Ignore()) 
      .ForMember(c => c.TechCategories, option => option.MapFrom(src => src.TechnologyTechCategories.Select(p => p.TechCategory.Id))); 

鑑於複雜到簡單的映射,您將無法在這裏使用ReverseMap。

爲了簡化映射(和您的對象模型),可能需要考慮更改您擁有的多對多映射。你真的需要那個'加入'班嗎?如果您在兩個類(Technology和TechCategory)上指定了IList,則FluentNHibernate自動映射(假設您正在使用此類)將爲您創建連接表。