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...
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.