This follows from stepanZ's answer ... I got this error when using Entity Framework Code First with AutoMapper .
When configuring AutoMapping we have the createddt , updateddt , createdby and updatedby fields that are automatically set in our public override int SaveChanges() function. In doing so, you need to ensure that these fields are ignored using AutoMapper , otherwise the database will be updated with null for these fields if they are not sent from View .
My problem was that I set the source and destination incorrectly, so I try to ignore the fields when installing ViewModel , and not when installing Model .
Mapping looked like this when I got this error (note: cfg.CreateMap<Source, Destination>() on the second line displays Model on ViewModel and sets Ignore() )
cfg.CreateMap<EventViewModel, Event>(); cfg.CreateMap<Event, EventViewModel>() .ForMember(dest => dest.CreatedBy, opt => opt.Ignore()) .ForMember(dest => dest.CreatedDt, opt => opt.Ignore()) .ForMember(dest => dest.UpdatedBy, opt => opt.Ignore()) .ForMember(dest => dest.UpdatedDt, opt => opt.Ignore());
The source and destination should be ignored for display from ViewModel To Model (note: the code below is correct where Ignore() placed against the display for ViewModel in Model )
cfg.CreateMap<Event, EventViewModel>(); cfg.CreateMap<EventViewModel, Event>() .ForMember(dest => dest.CreatedBy, opt => opt.Ignore()) .ForMember(dest => dest.CreatedDt, opt => opt.Ignore()) .ForMember(dest => dest.UpdatedBy, opt => opt.Ignore()) .ForMember(dest => dest.UpdatedDt, opt => opt.Ignore());
Gwasshoppa Jul 31 '16 at 22:48 2016-07-31 22:48
source share