2014-11-21 152 views
3

有了这2类为例:忽略导航属性时,映射Automapper

public class Product 
{ 
    public int Id {get; set;} 
    public int CategoryId {get; set;} 
} 

public class ProductDTO 
{ 
    public int Id {get; set;} 
    public int CategoryId {get; set;} 
    public Category Category {get; set;} 
} 

我怎能无视ProductDTO.Category bidrectionally映射时?

回答

2

假设你的意思是双向的,即两个类都有一个你想忽略的Category成员,你可以使用.ReverseMap()

映象

Mapper.CreateMap<Product, ProductDTO>()     
       .ForMember(dest => dest.Category, opt => opt.Ignore()).ReverseMap(); 

示例模型

public class Product 
    { 
     public int Id {get; set;} 
     public int CategoryId {get; set;} 
     public Category Category {get; set;} 
    } 

    public class ProductDTO 
    { 
     public int Id {get; set;} 
     public int CategoryId {get; set;} 
     public Category Category {get; set;} 
    } 

    public class Category 
    { 

    } 

Working Fiddle