【发布时间】:2019-09-30 10:24:32
【问题描述】:
我在 .NET CORE 2.2 项目中使用 AutoMapper。
我得到了这个例外:
缺少类型映射配置或不支持的映射。 映射类型: SaveFridgeTypeModel -> 冰箱类型 College.Refrigirator.Application.SaveFridgeTypeModel -> College.Refrigirator.Domain.FridgeType
在这一行:
var fridgeType = _mapper.Map<SaveFridgeTypeModel, FridgeType>(model);
这里是 FridgeType 类的定义:
public class FridgeType : IEntity , IType
{
public FridgeType()
{
Fridges = new HashSet<Fridge>();
}
public int ID { get; set; }
//Description input should be restricted
public string Description { get; set; }
public string Text { get; set; }
public ICollection<Fridge> Fridges { get; private set; }
}
这里是 SaveFridgeTypeModel 类的定义:
public class SaveFridgeTypeModel
{
public string Description { get; set; }
public string Text { get; set; }
}
我添加了这一行:
services.AddAutoMapper(typeof(Startup));
到 Startup 类中的 ConfigureServices 函数。
更新
我忘记在帖子中添加映射配置。
这是映射配置类:
public class ViewModelToEntityProfile : Profile
{
public ViewModelToEntityProfile()
{
CreateMap<SaveFridgeTypeModel, FridgeType>();
}
}
知道为什么会出现上述异常吗?
【问题讨论】:
-
你需要先配置映射,见:stackoverflow.com/q/40275195/1875256
-
正如错误所说,没有从
SaveFridgeModelType到FridgeType的映射。 -
@VidmantasBlazevicius 请查看更新
-
ViewModelToEntityProfile是否与Startup在同一个程序集中?通过写AddAutomapper(typeof(Startup)),你基本上说只从这个程序集中获取地图 -
我的意思是我猜阻力最小的路径是
AddAutomapper(typeof(ViewModelToEntityProfile))
标签: c# .net .net-core automapper