自动映射器映射异常
Automapper Mapping Exception
我正在尝试构建一个简单的应用程序,我可以在其中存储和检索有关某些设备及其用户的一些详细信息,例如库存。但是当我尝试显示设备及其所有者的列表时,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();
}
}
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; }
}
资产虚拟机
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;
}
最后,我 return 将 GetAssets 方法的结果发送到我的控制器中的视图:
var model = await assetRepository.GetAssets();
return View(model);
好吧,我发现我做错了什么。这就是我所做的:
因为我在 GetAssets 方法中查询数据库后得到了一个列表,所以我不得不将我的映射更改为:
var model = mapper.Map<List<AssetVM>>(asset);
为了能够 return 这个模型,我还必须将我的方法声明更改为:
public async Task<List<AssetVM>> GetAssets()
此更改使其有效,但我没有获得正在使用该资产的用户的详细信息。这是由于我的 AssetVM 视图模型中的拼写错误。
public AppUsersVM AppUser { get; set; }
这些就是我必须做的所有改变。要成为一名称职的程序员还有很长的路要走,所以如果您让我知道我的逻辑有任何缺陷或有任何建议,我会很高兴。
我正在尝试构建一个简单的应用程序,我可以在其中存储和检索有关某些设备及其用户的一些详细信息,例如库存。但是当我尝试显示设备及其所有者的列表时,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();
}
}
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; }
}
资产虚拟机
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;
}
最后,我 return 将 GetAssets 方法的结果发送到我的控制器中的视图:
var model = await assetRepository.GetAssets();
return View(model);
好吧,我发现我做错了什么。这就是我所做的:
因为我在 GetAssets 方法中查询数据库后得到了一个列表,所以我不得不将我的映射更改为:
var model = mapper.Map<List<AssetVM>>(asset);
为了能够 return 这个模型,我还必须将我的方法声明更改为:
public async Task<List<AssetVM>> GetAssets()
此更改使其有效,但我没有获得正在使用该资产的用户的详细信息。这是由于我的 AssetVM 视图模型中的拼写错误。
public AppUsersVM AppUser { get; set; }
这些就是我必须做的所有改变。要成为一名称职的程序员还有很长的路要走,所以如果您让我知道我的逻辑有任何缺陷或有任何建议,我会很高兴。