Emin
Emin

Reputation: 11

AutoMapper Exception: Missing type map configuration or unsupported mapping

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 -> IEnumerable1 System.Object -> System.Collections.Generic.IEnumerable1[[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

Answers (1)

Sarmad Georgie
Sarmad Georgie

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

Related Questions