ABP多语言映射未由AutoMapper ProjectTo调用。如果可行,它假定将翻译实体映射到DTO的name属性。它可以与我的旧代码一起使用,而无需使用ProjectTo。请参考下面的示例代码:
注意:我正在使用ABP版本4.8.1
注意:我已经在ProductDTO中包括了Translation DTO,以强制自动映射器的projectTo在生成IQueryable时急切地加载Translation数据。
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);
答案 0 :(得分:0)
您可以检查那些链接,它似乎是基于github的旧问题
https://github.com/aspnetboilerplate/aspnetboilerplate/issues/3356
这是一个关于StackOverFlow的问题。
Data localization in mapping in ASP.NET Zero
请让我知道其中一个是否适合您。