Reputation: 11
I have an ASP.Net Core C# application & using AutoMapper
DTO
public class ApplicationUserDto
{
public string Id { get; set; }
public string AdSoyad { get; set; }
public string UserName { get; set; }
public string UserId { get; set; }
public string Pass { get; set; }
}
Model
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
PassNotes = new Collection<PassNote>();
}
public string AdSoyad { get; set; }
public string Adres { get; set; }
public string Sehir { get; set; }
public string PostaKodu { get; set; }
public ICollection<PassNote> PassNotes { get; set; }
}
public class PassNote
{
[Key]
public int Id { get; set; }
[Required]
public string UserName { get; set; }
[Required]
public string Password { get; set; }
public string ApplicationUserId { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
AutoMapper Configuration
public class MapProfile:Profile
{
public MapProfile()
{
CreateMap<PassNote, PassNoteDto>();
CreateMap<PassNoteDto, PassNote>();
CreateMap<ApplicationUser,ApplicationUserDto>();
CreateMap<ApplicationUserDto, ApplicationUser>();
}
}
Signature
return await _appDbContext.ApplicationUser.Include(x => x.PassNotes).SingleOrDefaultAsync(x => x.Id == userId);
Controller
public async Task<IActionResult> List(string id)
{
var applicationUser = await _applicationUserService.GetWithUserByIdAsync(id);
return View(_mapper.Map<IEnumerable<ApplicationUserDto>>(applicationUser));
}
When the controller is return, it throws the below run time error.
AutoMapperMappingException: Missing type map configuration or unsupported mapping. Mapping types: Object -> IEnumerable
1 System.Object -> System.Collections.Generic.IEnumerable
1[[Recorder.Web.DTOs.ApplicationUserDto, Recorder.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
How to typecast/map this ?
Thanks!
Upvotes: 0
Views: 1766
Reputation: 113
I would need to know the signature of GetWithUserByIdAsync
but I'm going to assume that it returns a single user object. Automapper is just saying you can't map a single ApplicationUser
to a collection of ApplicationUserDto
. Try this instead:
public async Task<IActionResult> List(string id)
{
var applicationUser = await _applicationUserService.GetWithUserByIdAsync(id);
return View(_mapper.Map<ApplicationUserDto>(applicationUser));
}
You also need to make sure that the view accepts the correct model type.
Upvotes: 1