简体   繁体   English

automapper 将每个 map 源和目标添加到字典

[英]automapper add every map source and destination to dictionary

I have a model and a view model我有一个 model 和一个视图 model

public class CategoryViewModel{
    public string Serial { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
    public int ParentId { get; set; }
}

public class Category{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ParentId { get; set; }
    public Category Parent { get; set; }
}

and the CategoryViewModel is in treeview shape. CategoryViewModel是 treeview 形状。 I want to map CategoryViewModel to Category with Automapper and also add them to a dictionary as mentioned below:我想使用Automapper将 map CategoryViewModelCategory并将它们添加到字典中,如下所述:

Dictionary<string, OrganizationLevel> dic = new Dictionary<string, OrganizationLevel>();

Also, I want ot get the source view model serial as key and destination Category as value then add them to dictionary something like this:另外,我想获取源视图 model serial作为键和目标Category作为值,然后将它们添加到字典中,如下所示:

automapper.map<CategoryViewModel, Category>({
   config =>{ dic.add( categoryViewModel.serial, category ) }
},categoryViewmodelTree)

in mapper config add:在映射器配置中添加:

AfterMap((src, des) => dic.Add(src, des));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM