Automapper in a Core 3.1 console app

55 views
Skip to first unread message

gigasof...@gmail.com

unread,
Jun 5, 2020, 3:51:15 PM6/5/20
to AutoMapper-users
Hi, 
I am trying to use automapper in a console application to do some database maintenance. I figured since I used in my Core web api, I could simply use it in my Core console application.But no matter what I do, it seems like the profile cannot be found. The classes are almost identical. 

I get the following error...

AutoMapper.AutoMapperMappingException: 'Error mapping types
AutoMapperMappingException: Missing type map configuration or unsupported mapping.

I am pasting my code here, in hopes that somebody has tried to implement this in a console app with a service collection.

{

// Error on this line in my worker service
var genlst = _mapper.Map<IEnumerable<CompanyDatabaseClass>>(myList);


// Setup services

public static IServiceProvider _serviceProvider {
get;
set;
}

public static void Setup()
{
var configuration = new ConfigurationBuilder()
    .SetBasePath(AppContext.BaseDirectory)
    .AddJsonFile("appsettings.json")
    .Build();

//setup our DI
_serviceProvider = new ServiceCollection()
    .AddSingleton<ILogger>(logger)
    .AddSingleton<LoggerService>()
    .AddSingleton<IWork, Work>()
    .AddSingleton(configuration)
    .AddConfiguration(configuration.GetSection("Logging"))
    .AddConsole(options => {
        options.IncludeScopes = true;
    })
    .SetMinimumLevel(LogLevel.Debug))
    .AddTransient<IWork, Work>()
    .AddAutoMapper((serviceProvider, automapper) =>
    {
        automapper.AddProfile(new AutoMapperProfiles());
    }, typeof(CompanyContext).Assembly)
    .BuildServiceProvider();
}


// The profile class
public class AutoMapperProfiles : Profile
{
    public AutoMapperProfiles()
    {
        CreateMap<CompanyDatabaseClass, GenericCompany>()
            .ForMember(dest => dest.SICCode, opt => opt.MapFrom(src => src.SicCode))
            .ForMember(dest => dest.SICDesciption, opt => opt.MapFrom(src => src.SicDesciption));
    }
}
}


 If anybody has any suggestions I would greatly appreciate them.

Thanks,
Tom


Reply all
Reply to author
Forward
0 new messages