I'm trying to map object Id to an existing object using automapper and EF core.
My dto accepts LocationId only, but I'd like to map full Location object in WorkRecord entity if object with such id exists in database, otherwise controller will return BadRequest.
DTO:
public class WorkRecordCreateDto
{
[Required]
public int? LocationId { get; set; }
}
Model
public class WorkRecord
{
public int Id { get; set; }
public Location Location { get; set; }
}
Controller:
[HttpPost]
public ActionResult Post(WorkRecordCreateDto workRecordDto)
{
var workRecord = _mapper.Map<WorkRecord>(workRecordDto);
_repository.GetRepository<WorkRecord>().Add(workRecord);
_repository.SaveChanges();
return Ok();
}