2014-08-28 70 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自动映射(假设您正在使用此类)将为您创建连接表。