Solarion
Honorary Master
- Joined
- Nov 14, 2012
- Messages
- 28,051
- Reaction score
- 17,804
Hi guys. I've hit an AutoMapper snag and no way to resolve this so far. I am trying to map a domain entity to a dto and then return the list as a view model.
I feel like I am missing a step somewhere. With EF we can simply ProjectTo as such.
No such like though when trying this without Entity Framework.
This is the error I'm getting.

This is the Mapping.
Please can someone help fill in a piece of a puzzle that I am missing somewhere.
I feel like I am missing a step somewhere. With EF we can simply ProjectTo as such.
C#:
var employees = await _context.Employees
.ProjectTo<EmployeeDto>(_mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);
var vm = new EmployeeListVm
{
Employees = employees
};
return vm;
No such like though when trying this without Entity Framework.
This is the error I'm getting.

This is the Mapping.
C#:
public interface IMapFrom<T>
{
void Mapping(Profile profile) => profile.CreateMap(typeof(T), GetType());
}
C#:
public class MappingProfile : Profile
{
public MappingProfile()
{
ApplyMappingsFromAssembly(Assembly.GetExecutingAssembly());
}
private void ApplyMappingsFromAssembly(Assembly assembly)
{
var types = assembly.GetExportedTypes()
.Where(t => t.GetInterfaces().Any(i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapFrom<>)))
.ToList();
foreach (var type in types)
{
var instance = Activator.CreateInstance(type);
var methodInfo = type.GetMethod("Mapping");
methodInfo?.Invoke(instance, new object[] { this });
}
}
}
Please can someone help fill in a piece of a puzzle that I am missing somewhere.
