question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReverseMap creates object for navigation property

See original GitHub issue

Source/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:closed
  • Created 6 years ago
  • Comments:21 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
jbogardcommented, Jun 26, 2017

@danielgreen I pushed a new config option to allow you to ignore paths for a reverse map, see #2161

0reactions
lock[bot]commented, May 6, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Stop Automapper creating empty objects on virtual ...
1 Answer. For ReverseMap() AutoMapper creates a reverse mapping configuration that includes unflattening which results with creating new object ...
Read more >
ReverseMap creates object for navigation property
ReverseMap creates object for navigation property.
Read more >
Flattening
One of the common usages of object-object mapping is to take a complex object model and flatten it to a simpler model. You...
Read more >
Getting Started with AutoMapper in ASP.NET Core
AutoMapper can handle mapping properties A.B.C into ABC. By flattening our model, we create a more simplified object that won't require a lot...
Read more >
Relationship navigations - EF Core
Navigations are layered over foreign keys to provide a natural, object-oriented view for reading and manipulating relationships. By using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found