为什么我尝试使用 AutoMapper 时出现异常?
Why I get exception when I try to use AutoMapper?
我在 .NET CORE 2.2 项目中使用 AutoMapper。
我得到这个异常:
Missing type map configuration or unsupported mapping.
Mapping types:
SaveFridgeTypeModel -> FridgeType
College.Refrigirator.Application.SaveFridgeTypeModel ->
College.Refrigirator.Domain.FridgeType
在这一行:
var fridgeType = _mapper.Map<SaveFridgeTypeModel, FridgeType>(model);
这里是 FridgeType 的定义 class:
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 的定义 class:
public class SaveFridgeTypeModel
{
public string Description { get; set; }
public string Text { get; set; }
}
我添加这一行:
services.AddAutoMapper(typeof(Startup));
在启动中配置服务功能class。
更新
我忘记将 mappin 配置添加到 post。
这是映射配置 class:
public class ViewModelToEntityProfile : Profile
{
public ViewModelToEntityProfile()
{
CreateMap<SaveFridgeTypeModel, FridgeType>();
}
}
知道为什么会出现上述异常吗?
在向 DI 注册自动映射器时,您需要使用地图所在程序集中的类型。
AddAutomapper(typeof(ViewModelToEntityProfile));
如果您有多个带有映射的程序集 - 您可以使用另一个重载:
AddAutomapper(typeof(ViewModelToEntityProfile), typeof(SomeOtherTypeInOtherAssembly));
创建映射配置class后,您需要在Startup.cs
中添加AutoMapperConfiguration,如下所示:
public void ConfigureServices(IServiceCollection services) {
// .... Ignore code before this
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new ViewModelToEntityProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc();
}
我在 .NET CORE 2.2 项目中使用 AutoMapper。
我得到这个异常:
Missing type map configuration or unsupported mapping. Mapping types: SaveFridgeTypeModel -> FridgeType College.Refrigirator.Application.SaveFridgeTypeModel -> College.Refrigirator.Domain.FridgeType
在这一行:
var fridgeType = _mapper.Map<SaveFridgeTypeModel, FridgeType>(model);
这里是 FridgeType 的定义 class:
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 的定义 class:
public class SaveFridgeTypeModel
{
public string Description { get; set; }
public string Text { get; set; }
}
我添加这一行:
services.AddAutoMapper(typeof(Startup));
在启动中配置服务功能class。
更新
我忘记将 mappin 配置添加到 post。
这是映射配置 class:
public class ViewModelToEntityProfile : Profile
{
public ViewModelToEntityProfile()
{
CreateMap<SaveFridgeTypeModel, FridgeType>();
}
}
知道为什么会出现上述异常吗?
在向 DI 注册自动映射器时,您需要使用地图所在程序集中的类型。
AddAutomapper(typeof(ViewModelToEntityProfile));
如果您有多个带有映射的程序集 - 您可以使用另一个重载:
AddAutomapper(typeof(ViewModelToEntityProfile), typeof(SomeOtherTypeInOtherAssembly));
创建映射配置class后,您需要在Startup.cs
中添加AutoMapperConfiguration,如下所示:
public void ConfigureServices(IServiceCollection services) {
// .... Ignore code before this
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new ViewModelToEntityProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc();
}