I need to use the DB context to map a nested object based on the Name property which is a string. I'm trying to inject DB context into the Mapping Config but I get the following error:
System.MissingMethodException: Cannot dynamically create an instance of type 'ChatBotCore.Application.Bots.Common.MappingConfig'. Reason: No parameterless constructor defined.less constructor defined
public class MappingConfig : IRegister
{
private readonly IApplicationDbContext _context;
public MappingConfig(IApplicationDbContext context)
{
_context = context;
}
public void Register(TypeAdapterConfig config)
{
config.NewConfig<Bot, GetBotsResult>()
.Map(dest => dest.Channel, src => src.Channel.Name)
.Map(dest => dest.NluServiceProvider, src => src.NluServiceProvider.Name);
config.NewConfig<CreateBotCommand, Bot>()
.Map(dest => dest.Channel, src => GetChannelByName(src.Channel))
.Map(dest => dest.NluServiceProvider, src => GetNluServiceProviderByName(src.NluServiceProvider));
config.NewConfig<UpdateBotCommand, Bot>()
.Map(dest => dest.Channel, src => GetChannelByName(src.Channel))
.Map(dest => dest.NluServiceProvider, src => GetNluServiceProviderByName(src.NluServiceProvider));
}
private async Task<NluServiceProvider> GetNluServiceProviderByName(string nluServiceProvider)
{
return await _context.NluServiceProviders.FirstAsync(x => x.Name.ToLower() == nluServiceProvider.ToLower());
}
private async Task<Channel> GetChannelByName(string channel)
{
return await _context.Channels.FirstAsync(x => x.Name.ToLower() == channel.ToLower());
}
}