【发布时间】:2019-12-17 11:14:49
【问题描述】:
AutoMapper ProjectTo 未调用 ABP 多语言映射。如果它工作,它假设将翻译实体映射到 DTO 的 name 属性。它可以在不使用 ProjectTo 的情况下与我的旧代码一起使用。请参考下面的示例代码:
注意:我使用的是 ABP 4.8.1 版
注意:我已在 ProductDTO 中包含 Translation DTO,以强制自动映射器的 projectTo 在生成 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);
【问题讨论】:
-
欣赏是否有人能指出如何使其工作?我的意思是定制现有的 asp.net 样板代码。我相信 ProjectTo 是将 EF 核心实体映射到 DTO 时要走的路,因为它可以提高性能和生产力。
-
您找到解决问题的方法了吗?我对 ProjectTo 和多语言 Abp 有完全相同的问题...
标签: automapper aspnetboilerplate