0

I have updated the Automapper to 9.0 and the earlier code is broken. I was using

 <PackageReference Include="AutoMapper" Version="6.2.2" />
 <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="3.2.0" />

I am trying to map the List as per this code

 public class ProjectBudgetSummaryProfile : Profile
    {
        public ProjectBudgetSummaryProfile()
        {
            CreateMap<ProjectBudgetSummaryViewModel, HdsBudgetSummary>();
            CreateMap<ProjectBudgetSummaryViewModel, CdsBudgetSummary>();
        }
    }

var finance = _mapper.Map<List<ProjectBudgetSummaryViewModel>>(budgetSummaries)


 public class ProjectBudgetSummaryViewModel
    {
        public decimal? CurrentApproval { get; set; }
        public decimal? CurrentForecast { get; set; }
        public decimal? Delta { get; set; }
        public string FinancialItemDescription { get; set; }
        public decimal? ForecastDelta { get; set; }
        public decimal? PreviousForecast { get; set; }
        public long? ForecastId { get; set; }
    }

public class HdsBudgetSummary : BudgetSummary
    {
    }

 public class BudgetSummary
    {
        public decimal? CurrentApproval { get; set; }
        public decimal? CurrentForecast { get; set; }
        public decimal? Delta { get; set; }
        public string FinancialItemDescription { get; set; }
        public decimal? ForecastDelta { get; set; }
        public decimal? PreviousForecast { get; set; }
        public int ProjectNumber { get; set; }
        public long? ForecastId { get; set; }
    }

Configuration setup

 public virtual IServiceProvider ConfigureServices(IServiceCollection services)
        {
            ConfigureMapping(services);
            return ServiceProvider;
        }


protected virtual void ConfigureMapping(IServiceCollection services)
        {
            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies().Where(assembly => !assembly.FullName.StartsWith("Microsoft.VisualStudio.TraceDataCollector")));
        }

Error

Missing type map configuration or unsupported mapping.

Mapping types:
HdsBudgetSummary -> ProjectBudgetSummaryViewModel
ProjectPerformanceReview.DomainModel.ComplexDomainModel.Hds.HdsBudgetSummary -> ProjectPerformanceReview.WebApi.ViewModels.BudgetSummary.ProjectBudgetSummaryViewModel

Some of the links I followed Mapping Lists using Automapper

Automapper 8 mapping not working properly

San Jaisy
  • 15,327
  • 34
  • 171
  • 290

1 Answers1

2

Your profile is only mapping from ProjectBudgetSummaryViewModel to HdsBudgetSummary. If you want it to map in the opposite direction as well, you have to extend the profile.

public ProjectBudgetSummaryProfile()
{
   CreateMap<ProjectBudgetSummaryViewModel, HdsBudgetSummary>()
      ReverseMap();
   CreateMap<ProjectBudgetSummaryViewModel, CdsBudgetSummary>()
      ReverseMap();
}

FYI: You don't need to return IServiceProvider from the ConfigureServices delegate.

alsami
  • 8,996
  • 3
  • 25
  • 36