开发者

AutoMapper Model To ViewModel Vice Versa Updating Record

I have a unit test which I use mapper from Model to ViewModel, and ViewModel back to Model the Update the record - but in reality I haven't changed a thing, just testing that updating works.

        [TestMethod]
    public void UpdateEntry()
    {
        //Map Model to ViewModel
        Mapper.CreateMap<GlobalExport, GlobalExportViewModel>();
        Mapper.CreateMap<FileNameSetup, FileNameSetupViewModel>();
        Mapper.CreateMap<FileNameVariable, FileNameVariableViewModel>();
        Mapper.CreateMap<EmailSetup, EmailSetupViewModel>();
        Mapper.CreateMap<SelectedSection, SelectedSectionViewModel>();
        Mapper.CreateMap<AvailableSection, AvailableSectionViewModel>();
        Mapper.CreateMap<AvailableColumn, AvailableColumnViewModel>();
        Mapper.CreateMap<Policy, PolicyViewModel>();
        Mapper.CreateMap<SelectedColumn, SelectedColumnViewModel>();
        Mapper.CreateMap<SelectedEmployeeSeaServiceType, SelectedEmployeeSeaServiceTypeViewModel>();
        Mapper.CreateMap<SelectedEmployeeStatus, SelectedEmployeeStatusViewModel>();
        Mapper.CreateMap<EmployeeStatus, EmployeeStatusViewModel>();
        Mapper.CreateMap<EmployeeSubStatus, EmployeeSubStatusViewModel>();
        Mapper.CreateMap<SelectedEmployeeSubStatus, SelectedEmployeeSubStatusViewModel>();
        Mapper.CreateMap<SelectedSeaServiceType, SelectedSeaServiceTypeViewModel>();


        GlobalExport globalExport = _service.GetGlobalExportById(1);
        GlobalExportViewModel globalExportViewModel = Mapper.Map<GlobalExport, GlobalExportViewModel>(globalExport);

        //Map ViewModel to Model
        Mapper.CreateMap<GlobalExportViewModel, GlobalExport>();
        Mapper.CreateMap<FileNameSetupViewModel, FileNameSetup>();
        Mapper.CreateMap<FileNameVariableViewModel, FileNameVariable>();
        Mapper.CreateMap<EmailSetupViewModel, EmailSetup>();
        Mapper.CreateMap<SelectedSectionViewModel, SelectedSection>();
        Mapper.CreateMap<AvailableSectionViewModel, AvailableSection>();
        Mapper.CreateMap<AvailableColumnViewModel, AvailableColumn>();
        Mapper.CreateMap<PolicyViewModel, Policy>();
        Mapper.CreateMap<SelectedColumnViewModel, SelectedColumn>();
        Mapper.CreateMap<SelectedEmployeeSeaServiceTypeViewModel, SelectedEmployeeSeaServiceType>();
        Mapper.CreateMap<SelectedEmployeeStatusViewModel, SelectedEmployeeStatus>();
        Mapper.CreateMap<EmployeeStatusViewModel, EmployeeStatus>();
        Mapper.CreateMap<EmployeeSubStatusViewModel, EmployeeSubStatus>();
        Mapper.CreateMap<SelectedEmployeeSubStatusViewModel, SelectedEmployeeSubStatus>();
        Mapper.CreateMap<Se开发者_StackOverflowlectedSeaServiceTypeViewModel, SelectedSeaServiceType>();

        GlobalExport newGlobalExport = Mapper.Map<GlobalExportViewModel, GlobalExport>(globalExportViewModel);
        _service.UpdateGlobalExport(newGlobalExport);

    }

I get this error:

AutoMapper Model To ViewModel Vice Versa Updating Record

Can somebody give some opinion or better a solution :P Thank you very much!


The problem here is that the GlobalExportRepository.UpdateGlobalExport method is attempting to attach a GlobalExport object to the same DbContext instance that retrieved it.

Try modifying the GlobalExportRepository.UpdateGlobalExport method to explicitly mark the entity as modified and simply call the DbContext.SaveChanges method.

Here's an example:

public void UpdateGlobalExport(GlobalExport instance)
{
    var entity = context.Entry(instance);
    entity.State = EntityState.Modified;        
    context.SaveChanges();
}

Any changes made to the GlobalExport instance will automatically be detected even if its state is Detached.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜