亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么當我嘗試使用 AutoMapper 時出現異常?

為什么當我嘗試使用 AutoMapper 時出現異常?

PHP
慕無忌1623718 2024-01-20 21:44:50
我在 .NET CORE 2.2 項目中使用 AutoMapper。我得到這個異常:缺少類型映射配置或映射不受支持。映射類型:SaveFridgeTypeModel -> FridgeType 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>();    }}知道為什么我會得到上面的異常嗎?
查看完整描述

2 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

在使用 DI 注冊自動映射器時,您需要使用映射所在程序集中的類型。

AddAutomapper(typeof(ViewModelToEntityProfile));

如果您有多個帶有地圖的程序集 - 您可以使用另一個重載:

AddAutomapper(typeof(ViewModelToEntityProfile), typeof(SomeOtherTypeInOtherAssembly));



查看完整回答
反對 回復 2024-01-20
?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

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

}


查看完整回答
反對 回復 2024-01-20
  • 2 回答
  • 0 關注
  • 185 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號