1
votes

The ABP multi lingual mapping was not being called by AutoMapper ProjectTo. If it work, it suppose to map the translation entity to DTO's name property. It works with my old code without using ProjectTo. Please refer to the sample code below:

NOTE : I am using ABP version 4.8.1

NOTE : I have included the Translation DTO in ProductDTO to force auto mapper's projectTo to eagerly load Translation data when generating the IQueryable.

ProductAppService.cs

public async Task<ICollection<ProductListDto>> GetAll()
{
     return await repository.GetAll().ProjectTo<ProductListDto>().ToListAsync();
}

Product.cs

public class Product : Entity, IMultiLingualEntity<ProductTranslation>
{
    public ICollection<ProductTranslation> Translations { get; set; }
}

ProductTranslation.cs

public class ProductTranslation : Entity, IEntityTranslation<Product>
{
    public string Name { get; set; }
}

ProductDto.cs

public class ProductListDto
{
    // Mapped from ProductTranslation.Name
    public string Name { get; set; }

    // Purposely include the translations dto here to force automapper to eagerly load the translation 
    // data from DB
    [JsonIgnore]
    public ICollection<ProductTranslationDto> Translations { get; set; }
}

Module.cs

public override void PreInitialize()
{
    Configuration.Modules.AbpAutoMapper().Configurators.Add(cfg =>
    {
        MultiLingualMapContext context = new MultiLingualMapContext(IocManager.Resolve<ISettingManager>());
        cfg.DisableConstructorMapping();
        cfg.AddCollectionMappers();

        CustomDtoMapper.CreateMappings(cfg);
    });
}

CustomDtoMapper.cs

cfg.CreateMultiLingualMap<Product, ProductTranslation, ProductListDto>(context);
1
Appreciate if anyone can pin point how to make it works? I mean customisation of the existing asp.net boilerplate code. I believe ProjectTo is the way to go when mapping EF core entities to DTOs because it improve performance and productivity. - kenn3th
Have you found a solution to the problem? I have exactly the same problem with ProjectTo and multilingual Abp... - Adrian

1 Answers