如何将对象 ID 从 DTO 映射到数据库中的现有对象?

How to map object Id from DTO to existing object in database?

我正在尝试使用自动映射器和 EF 核心将对象 ID 映射到现有对象。 我的 dto 只接受 LocationId,但如果数据库中存在具有此类 ID 的对象,我想在 WorkRecord 实体中映射完整的 Location 对象,否则控制器将 return BadRequest.

DTO:

    public class WorkRecordCreateDto
    {
        [Required]
        public int? LocationId { get; set; }
    }

型号

    public class WorkRecord
    {
        public int Id { get; set; }
        public Location Location { get; set; }
    }

控制器:

        [HttpPost]
        public ActionResult Post(WorkRecordCreateDto workRecordDto)
        {
            var workRecord = _mapper.Map<WorkRecord>(workRecordDto);
            _repository.GetRepository<WorkRecord>().Add(workRecord);
            _repository.SaveChanges();
            return Ok();

        }

为此我们有一个 EnityConverter:

public class EntityConverter<TDestination> : IValueConverter<int, TDestination>
{
    private readonly ISession session;

    public EntityConverter(ISession session)
    {
        this.session = session;
    }

    public TDestination Convert(int sourceMember, ResolutionContext context)
    {
        return session.Get<TDestination>(sourceMember);
    }
}

要使用它,您必须配置映射器,它可以使用 DependencyInjection。我们使用 Grace,它在您的环境中会有所不同:

container.Configure(c => c.ExportFuncWithContext<IMapper>((scope, staticContext, context)
                    => new Mapper(automapperConfig, t => scope.Locate(t))).Lifestyle.SingletonPerScope());

现在您可以在 AutoMapper 映射中添加 EntityConverter:

CreateMap<WorkRecord, WorkRecordCreateDto>()
            .ReverseMap()
            .ForMember(d => d.Location, opts => opts.ConvertUsing<EntityConverter<WorkRecord>, int>(src => src.LocationId));