簡體   English   中英

在Automapper中映射時忽略導航屬性

[英]Ignore navigation property when mapping in 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;}
}

在映射bidrectionally時如何忽略ProductDTO.Category

假設你的意思是雙向的,即兩個類都有一個你想忽略的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
    {

    }

工作小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM