Automapper - 映射子类

时间:2015-04-04 13:15:21

标签: c# automapper

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

/// <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

所以它告诉我,我没有为Lis​​tingShippingCost提供映射,即使我确实有关于ListingShippingCost的映射定义,正如你所看到的那样。显然我需要为我的SellerListing定义添加一些用于此映射的内容,但我不确定是什么。

1 个答案:

答案 0 :(得分:2)

您应该定义从哪里映射 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>();
}