复杂对象/嵌套映射中的自动映射和映射列表

时间:2014-07-17 17:31:27

标签: c# automapper-3

我有点时间从旧版映射标准转换为自动播放器。

这是我的课程

// Models
public class BaseModel
{
    public Int64 Id { get; set; }
    public Guid UniqueId { get; set; }
    public DateTime? CreateDate { get; set; }
    public DateTime? LastUpdate { get; set; }
} 

public class LibraryItemModel : BaseModel
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string URL { get; set; }
    public bool IsActive { get; set; }
    public List<LibraryCategoryModel> Categories { get; set; }
}   

public class LibraryCategoryModel : BaseModel
{
    public string Description { get; set; }
}


// Entity Classes

public partial class LibraryItem
{
    public LibraryItem()
    {
        this.LibraryItemCategories = new HashSet<LibraryItemCategory>();
    }

    public long Id { get; set; }
    public System.Guid UniqueId { get; set; }
    public string Description { get; set; }
    public string URL { get; set; }
    public System.DateTime CreateDate { get; set; }
    public System.DateTime LastUpdate { get; set; }
    public bool IsActive { get; set; }
    public string Name { get; set; }

    public virtual ICollection<LibraryItemCategory> LibraryItemCategories { get; set; }
}
// comes from a ternary table in DB... many to many
public partial class LibraryItemCategory
{
    public long LibraryItemId { get; set; }
    public long LibraryCategoryId { get; set; }
    public System.DateTime CreateDate { get; set; }
    public System.DateTime LastUpdate { get; set; }

    public virtual LibraryCategory LibraryCategory { get; set; }
    public virtual LibraryItem LibraryItem { get; set; }
}

public partial class LibraryCategory
{
    public LibraryCategory()
    {
        this.LibraryCategoryRoles = new HashSet<LibraryCategoryRole>();
        this.LibraryItemCategories = new HashSet<LibraryItemCategory>();
    }

    public long id { get; set; }
    public System.Guid UniqueId { get; set; }
    public string Description { get; set; }
    public System.DateTime CreateDate { get; set; }
    public System.DateTime LastUpdate { get; set; }

    public virtual ICollection<LibraryCategoryRole> LibraryCategoryRoles { get; set; }
    public virtual ICollection<LibraryItemCategory> LibraryItemCategories { get; set; }
}


    // Old Conversion code doing it the long way

    private LibraryItemModel Convert(Entities.LibraryItem libraryItem)
    {

        var newLibraryItem = new LibraryItemModel
        {
            UniqueId = libraryItem.UniqueId,
            Name = libraryItem.Name,
            Description = libraryItem.Description,
            URL = libraryItem.URL,
            CreateDate = libraryItem.CreateDate,
            LastUpdate = libraryItem.LastUpdate,
            IsActive = libraryItem.IsActive,
            Categories = new List<LibraryCategoryModel>()
        };

        foreach (var lc in libraryItem.LibraryItemCategories)
        {
            var newCategory = new LibraryCategoryModel
            {
                UniqueId = lc.LibraryCategory.UniqueId,
                Description = lc.LibraryCategory.Description,
                CreateDate = lc.LibraryCategory.CreateDate,
                LastUpdate = lc.LibraryCategory.LastUpdate
            };

            newLibraryItem.Categories.Add(newCategory);
        }

        return newLibraryItem;
    }


    // My attempt at automapper to go between the models and entities
    Mapper.CreateMap<EF.Entities.LibraryItem, LibraryItemModel>();
    Mapper.CreateMap<LibraryItemModel, EF.Entities.LibraryItem>();
          .ForMember(lim => lim.LibraryItemCategories, o => o.Ignore()
    Mapper.CreateMap<EF.Entities.LibraryCategory, LibraryCategoryModel>();
    Mapper.CreateMap<LibraryCategoryModel, EF.Entities.LibraryCategory>()
          .ForMember(lcm => lcm.LibraryCategoryRoles, o => o.Ignore())
          .ForMember(lcm => lcm.LibraryItemCategories, o => o.Ignore());

无论我如何配置忽略或自定义映射,它似乎都不喜欢这种嵌套。任何Automapper专家都可以告诉我如何使用这样的复杂对象进行映射。 enitity类是通过EF6 edmx文件生成的。

2 个答案:

答案 0 :(得分:9)

所以基本上这里的问题是你想要从属于LibraryItemCategory的每个LibraryItem映射到LibraryCategoryModel,其中包含来自每个LibraryItemCatalog&#39的属性; s LibraryCatalog财产。

首先,您要将集合正确映射到彼此:

Mapper.CreateMap<LibraryItem, LibraryItemModel>()
    .ForMember(
        dest => dest.Categories, 
        opt => opt.MapFrom(src => src.LibraryItemCategories));

接下来,您需要担心将LibraryItemCategory内的每个LibraryItem.LibraryItemCategories映射到LibraryCatalogModel。如问题中所述,您需要访问每个LibraryItemCategory的{​​{1}}媒体资源,并实际从中进行映射。这看起来的方式是:

LibraryCatalog

在这里,我们告诉AutoMapper要从Mapper.CreateMap<LibraryItemCategory, LibraryCategoryModel>() .ConstructUsing(ct => Mapper.Map<LibraryCategoryModel>(ct.LibraryCategory)) .ForAllMembers(opt => opt.Ignore()); 映射到LibraryItemCategory,我们需要使用另一个<{1}}来构建/ em>在内部LibraryCategoryModel属性上调用LibraryCategoryModel

接下来,剩下要做的就是定义从Mapper.MapLibraryCategory的映射:

LibraryCategory

现在,LibraryCategoryModel上的Mapper.CreateMap<LibraryCategory, LibraryCategoryModel>(); 来电应该为您处理一切。


或者,您可以将地图从Mapper.Map移除到LibraryItem,然后使用LINQ创建实际要映射的LibraryItemCategory集合,从{LibraryCategoryModel映射定义1}}到LibraryCategory

LibraryItem

您显然仍然需要从LibraryItemModelMapper.CreateMap<LibraryItem, LibraryItemModel>() .ForMember( dest => dest.Categories, opt => opt.MapFrom( src => src.LibraryItemCategories.Select(lb => lb.LibraryCategory))); 的映射,但您可能更喜欢这种映射,因为它涉及更少的映射。

答案 1 :(得分:1)

尝试类似

的内容
Mapper
    .CreateMap<LibraryItemModel, EF.Entities.LibraryItem>()
    .ForMember(
        dest => dest.LibraryItemCategories,
        opt => opt.MapFrom(src => src.LibraryItemCategories )
    );

所以你声明你的嵌套属性将被映射到哪里。

您可以在documentation site

上找到另一个示例