ReverseMap creates object for navigation property
See original GitHub issueSource/destination types
public partial class TimesheetModel
{
public int ID { get; set; }
public DateTime? StartDate { get; set; }
public int? Contact { get; set; }
public ContactModel ContactNavigation { get; set; }
}
public class TimesheetViewModel
{
public int? Contact { get; set; }
public DateTime? StartDate { get; set; } = DateTime.Now;
}
Mapping configuration
CreateMap<TimesheetModel, TimesheetViewModel>().ReverseMap();
However, if I use the following mapping configuration, without ReverseMap, the issue does not occur:
CreateMap<TimesheetModel, TimesheetViewModel>();
CreateMap<TimesheetViewModel, TimesheetModel>();
Version: x.y.z
AutoMapper 6.1.0, netcoreapp 1.1, EF Core 1.1.2
Expected behavior
In 6.0.2, when mapping from TimesheetViewModel to TimesheetModel (i.e. via the reverse map), the TimesheetModel object would be created with ID = 0, the Contact and StartDate values copied over, and the ContactNavigation property set to null.
Actual behavior
In 6.1.0, the ContactNavigation property is set to an instance of ContactModel, presumably from a call to new ContactModel() within AutoMapper. No values are set for any property of ContactModel.
When the TimesheetModel is subsequently added to the EF Core db context, EF also sees the related Contact entity as new adds it to the change tracker. When attempt to save, it fails because of some non-nullable columns on the Contact table that we have not set values for on the Contact entity.
Steps to reproduce
// timesheetViewModel constructed by MVC model binding
var timesheetModel = _mapper.Map<TimesheetModel>(timesheetViewModel);
Issue Analytics
- State:
- Created 6 years ago
- Comments:21 (14 by maintainers)
@danielgreen I pushed a new config option to allow you to ignore paths for a reverse map, see #2161
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.