【发布时间】:2021-03-25 13:06:03
【问题描述】:
我安装了 AutoMapper.Extensions.Microsoft.DependencyInjection nuget 包,并且我使用的是 .NET Core 3.1。根据文档 (https://docs.automapper.org/en/stable/Configuration.html),我应该能够为我的属性名称添加前缀。但是当我映射我的两个类时,带有前缀的属性为空。我有一个具有多个属性的抽象类,这些属性扩展到我的 UserDto 模型然后我将 UserDto 模型映射到没有抽象类的 UserEntity 模型。我的 UserEntity 模型对每个属性都使用前缀 User,例如UserId、UserName,而我的 UserDto 只使用 Id、Name 等。我注册 AutoMapper 并添加我的配置如下:
#region Auto Mapper
services.AddAutoMapper(config =>
{
config.RecognizePrefixes(new[] { "User", "Role", "Language" });
config.AddProfile(new DtoToEntityProfile());
config.AddProfile(new ContractToDtoProfile());
}, typeof(Startup));
#endregion
我将我的个人资料添加到配置中,看看这是否可行。在 Automapper 还可以通过扫描程序集自动找到我的配置文件之前。在不手动映射每个属性的情况下,我需要做什么才能将我的 Dto 映射到我的实体模型?
【问题讨论】:
标签: c# dependency-injection automapper