Home > Enterprise >  Automapper Mapping Exception
Automapper Mapping Exception

Time:12-31

I am trying to build a simple application where I can store and retrieve some details about some devices and the users of them, like an inventory. But when I try to display the list of devices with their owners, Automapper throws this error:

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

I don't understand what I am doing wrong here. How can I deal with this?

Startup.cs

builder.Services.AddAutoMapper(typeof(MapConfig));
builder.Services.AddControllersWithViews();

var app = builder.Build();

MapConfig.cs

public class MapConfig : Profile
{
    public MapConfig()
    {
        CreateMap<Asset, AssetVM>().ReverseMap();
        CreateMap<AppUser, AppUsersVM>().ReverseMap();
    }
}

Asset.Cs

public class Asset
{
    public int Id { get; set; }
    public string Brand { get; set; }
    public string Model { get; set; }
    public string? ProductNumber { get; set; }
    public string? SerialNumber { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateModified { get; set; }
    public bool IsAssigned { get; set; }
    public string? ISN { get; set; }
    public string Status { get; set; }
    public bool IsInsured { get; set; }
    public string Condition { get; set; }

    [ForeignKey("UserId")]
    public AppUser AppUser { get; set; }
    public string? UserId { get; set; }
}

AssetVM

   public class AssetVM
    {
    public int Id { get; set; }

    public string Brand { get; set; }

    public string Model { get; set; }
    
    [Display(Name ="Product Number")]
    public string? ProductNumber { get; set; }

    [Display(Name ="Serial Number")]
    public string? SerialNumber { get; set; }

    [Display(Name ="Date Created")]
    [DataType(DataType.Date)]
    public DateTime DateCreated { get; set; }

    [Display(Name = "Date Modified")]
    [DataType(DataType.Date)]
    public DateTime DateModified { get; set; }

    [Display(Name ="Assigned")]
    public bool IsAssigned { get; set; }

    public string? ISN { get; set; }

    [Required]
    public string Status { get; set; }

    [Display(Name ="Has Insurance")]
    public bool IsInsured { get; set; }

    [Required]
    public string Condition { get; set; }


    public string? UserId { get; set; }
    public SelectList? AppUsersList { get; set; }

    public AppUsersVM AppUsers { get; set; }
}

This is how I get and map the data that is to be displayed on the page:

    public async Task<AssetVM> GetAssets()
    {
        var asset = await context.Assets.Include(q => q.AppUser).ToListAsync();

        var model = mapper.Map<AssetVM>(asset);
        return model;
    }

And finally I return the result of GetAssets method to the view in my controller:

        var model = await assetRepository.GetAssets();
        return View(model);

CodePudding user response:

Well, I found what I'm doing wrong. This is what I've done:

Since I was getting a list after querying the database in my GetAssets method, I had to change my mapping to:

var model = mapper.Map<List<AssetVM>>(asset);

And to be able to return this model, I also had to change my method declaration to:

public async Task<List<AssetVM>> GetAssets()

This changes made it work, except I didn't get the details of the user that is using that asset. And this was due to a typo in my AssetVM viewmodel.

public AppUsersVM AppUser { get; set; }

Those were all the changed I had to do. Still have a looong way to be a competent programmer so I'd be glad if you let me know if I have any flaws in my logic or any recommendations at all.

  • Related