Automapper - 映射子类

Automapper - Mapping subclasses

我有一个方法定义了一些映射如下:

/// <summary>
/// Mapping definitions for domain entities to database entities
/// </summary>
private static void DefineMappingDomainEntitiesToDatabase()
{
    // SellerListing 
    Mapper.CreateMap<SellerListing, Data.SellerListing>();

    // ListingShippingCost
    Mapper.CreateMap<ListingShippingCost, Data.ListingShippingCost>();
}

SellerListing 实体定义包含以下内容属性

List<ListingShippingCost> ListingShippingCostList { get; set; }

当我调用 Mapper.AssertConfigurationIsValid(); 时,出现以下错误:

SellerListing -> SellerListing (Destination member list)
MyApp.Entities.SellerListing -> MyApp.Data.SellerListing (Destination member list)

Unmapped properties:
ListingShippingCost

所以它告诉我我没有 ListingShippingCost 的映射,尽管我确实有 ListingShippingCost 的映射定义,如您所见。显然我需要为这个映射添加一些东西到我的 SellerListing 定义中,但我不确定是什么。

您应该定义从哪里映射 ListingShippingCost 属性:

private static void DefineMappingDomainEntitiesToDatabase()
{
    // SellerListing 
    Mapper.CreateMap<SellerListing, Data.SellerListing>()
          .ForMember(x => x.ListingShippingCost, cfg => cfg.MapFrom(y => y.ListingShippingCostList));

    // ListingShippingCost
    Mapper.CreateMap<ListingShippingCost, Data.ListingShippingCost>();
}