I have all my db entities in Infrastructure
project (important: it's db-first approach). My dto entities are located in Application
project. What I want to do is to map Db entities to Dto entities but when I do so I get an error:
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping. Mapping types: List 1 -> UserDto System.Collections.Generic.List`1[[RegionManagement.Persistence.ModelsDb.User, RegionManagement.Persistence, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> RegionManagement.Domain.Entities.Administration.UserEntries.UserDto
At the first glance everything looks ok and I wonder where the problem lies.
I do the mapping in 2 places in my app: in Infrastructure
project (db entity -> dto entity) and Application
project (dto entity -> other dto/vm entities). Everything worked fine until I added mapping in Infrastucture
. So basically I have db entity User
and dto entity UserDto
. I created a repository in Infrastructure
project where I map those entities and mapping profile in seperate class:
UserRepository.cs
public class UserRepository : IUserRepository<UserDto>
{
protected readonly RegionManagementDbContext _dbContext;
private readonly IMapper _mapper;
public UserRepository(RegionManagementDbContext dbContext, IMapper mapper)
{
_dbContext = dbContext;
_mapper = mapper;
}
public async Task<IReadOnlyList<UserDto>> ListAllAsync()
{
var dbDataList = await _dbContext.Users.ToListAsync();
var mapList = _mapper.Map<UserDto>(dbDataList);
return (IReadOnlyList<UserDto>)mapList;
}
}
MappingProfile.cs
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<User, UserDto>();
}
}
and the I register service as an extension of IServiceCollection
(I still do it in Infrastructure project), as shown below:
InfrastructureServiceRegistration.cs
public static class InfrastructureServiceRegistration
{
public static IServiceCollection AddPersistenceServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<RegionManagementDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("RegionManagementConnectionString")));
services.AddAutoMapper(Assembly.GetExecutingAssembly());
services.AddScoped<IUserRepository<UserDto>, UserRepository>();
return services;
}
}
Startup.cs
(AddApplicationServices
is where I registered AutoMapper
for Application
project):
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationServices();
services.AddInfrastructureServices(Configuration);
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "RegionManagement.Api", Version = "v1" });
});
}
Error indicates that something is wrong with mapping User
-> UserDto
but have no idea what it could be.
I scan assembly for profiles in this place: services.AddAutoMapper(Assembly.GetExecutingAssembly());
but it doesn't help.
Error
Missing type map configuration or unsupported mapping. Mapping types: List 1 -> UserDto
says that you're trying to map a List into a UserDto.
This is the line with the problem:
var mapList = _mapper.Map<UserDto>(dbDataList);
Try to rewrite it this way:
var mapList = dbDataList.Select(_mapper.Map<UserDto>)