【发布时间】:2019-06-23 10:10:47
【问题描述】:
我需要将来自 API 的模型对象映射到 DbContext 上的实际实体对象。它在使用 POST 操作创建新机器对象时使用。
与往常一样,我为源/目标对象创建了一个简单的地图。 在这种情况下,我们将源对象视为 API 模型,将目标对象视为实体。此外,模型只有实体属性的一个子集。
来源/目的地类型
// Destination (entity on DbContext)
public class Machine
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MnmConfiguration { get; set; }
public MachineBootStatus Status { get; set; }
public long MachineDriverId { get; set; }
public MachineDriver MachineDriver { get; set; }
public string DriverConfiguration { get; set; }
public string DriverStatus { get; set; }
}
// Source (from API controller)
public class MachineCreateModel
{
public string Name { get; set; }
public string Description { get; set; }
public string MnmConfiguration { get; set; }
public long MachineDriverId { get; set; }
}
映射配置
public class DomainProfile : Profile
{
public DomainProfile()
{
//CreateMap<MachineCreateModel, Machine>();
// Update 2019/01/30 with proposed solution
CreateMap<MachineCreateModel, Machine>(MemberList.Source);
}
}
我正在使用 Unity DI 容器,AutoMapper 的配置是这样的:
container = new UnityContainer();
// ... some other configurations...
container.RegisterType<IMapper, Mapper>(new InjectionConstructor(new MapperConfiguration(cfg => cfg.AddProfile<DomainProfile>())));
版本
使用 AutoMapper v8.0.0。
预期行为
我希望获得一个没有错误的简单自动映射,因为我的源模型只是目标模型属性的子集,具有相同的名称。
实际行为
当我点击这行代码时,我收到关于未映射属性的错误:
Machine entity = Mapper.Map<Machine>(request.Machine);
[14:08:34.363 8 2e62361a INF] Creating new machine: TEST M1
[14:08:36.205 8 bd577466 ERR] An unhandled exception has occurred while executing the request.
AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
=================================================================================================
AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
MachineCreateModel -> Machine (Destination member list)
MyApplication.Dcs.Application.Models.MachineCreateModel -> MyApplication.Dcs.Domain.Entities.Machine (Destination member list)
Unmapped properties:
Id
Status
MachineDriver
DriverConfiguration
DriverStatus
at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps)
at lambda_method(Closure , MachineCreateModel , Machine , ResolutionContext )
at lambda_method(Closure , Object , Object , ResolutionContext )
at AutoMapper.Mapper.AutoMapper.IMapper.Map[TDestination](Object source)
at MyApplication.Dcs.Application.Commands.MachineCreateCommandHandler.Handle(MachineCreateCommand request, CancellationToken cancellationToken) ..Commands\MachineCreateCommand.cs:line 28
注意
在我的解决方案中,我有很多项目,其中 3 个正在使用 AutoMapper(所有人的版本相同)。有 3 个不同的 DomainProfile.cs 文件(每个项目 1 个)具有所需的映射。
在其他 2 个 DomainProfile 类中,我有一些手动映射(参见下面的示例),因为我需要将具有意大利属性名称的对象“翻译”为另一个具有英文属性名称的对象。所以每个对象映射有很多行,比如:
CreateMap<ArticleCreateUpdateModel, Articoli>()
.ForMember(d => d.Categoria, opt => opt.MapFrom(src => src.Category))
.ForMember(d => d.CodiceArticolo, opt => opt.MapFrom(src => src.Code))
.ForMember(d => d.Descrizione, opt => opt.MapFrom(src => src.Description))
.ForMember(d => d.Famiglia, opt => opt.MapFrom(src => src.Family))
.ForMember(d => d.Note, opt => opt.MapFrom(src => src.Note))
...
我不知道这些手动成员映射到一个或多个 DomainProfile 类上的使用是否会以某种方式迫使我始终解释所有后续映射,即使它们应该像本示例中的那样简单。
【问题讨论】:
标签: c# .net-core mapping entity-framework-core automapper