【问题标题】:Automapper Mapping ExceptionAutomapper 映射异常
【发布时间】:2022-01-26 23:13:21
【问题描述】:

我正在尝试构建一个简单的应用程序,我可以在其中存储和检索有关某些设备及其用户的一些详细信息,例如清单。但是当我尝试显示设备列表及其所有者时,Automapper 会抛出此错误:

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

我不明白我在这里做错了什么。我该如何处理?

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();
    }
}

资产.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; }
}

资产虚拟机

   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; }
}

这就是我获取和映射要在页面上显示的数据的方式:

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

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

最后我将 GetAssets 方法的结果返回到我的控制器中的视图:

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

【问题讨论】:

    标签: c# asp.net-core automapper model-binding .net-6.0


    【解决方案1】:

    好吧,我发现我做错了什么。这就是我所做的:

    由于我在 GetAssets 方法中查询数据库后得到了一个列表,因此我不得不将映射更改为:

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

    为了能够返回这个模型,我还必须将我的方法声明更改为:

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

    此更改使其正常工作,但我没有获得使用该资产的用户的详细信息。这是由于我的 AssetVM 视图模型中的拼写错误。

    public AppUsersVM AppUser { get; set; }
    

    这些都是我必须做的改变。成为一名称职的程序员还有很长的路要走,所以如果你让我知道我的逻辑是否有任何缺陷或任何建议,我会很高兴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多