【问题标题】:Map two identical models, nested with automapper映射两个相同的模型,使用自动映射器嵌套
【发布时间】:2021-01-20 05:11:41
【问题描述】:

我做了一些研究,但找不到我想要的。

我有无穷无尽的菜单。我有一个用于此菜单的 MenuDTO 和一个 MenuViewModel。我在模型和 DTO 之间匹配没有问题,但是在将 DTO 映射到 ViewModel 时遇到了问题。显然我找不到解决方案,你能帮忙吗?

我的 MenuDTO 对象

    public class MenuDto : BaseDto
    {
        public string Name { get; set; }
        public string Icon { get; set; }
        public string Order { get; set; }
        public string Url { get; set; }
        public bool IsVisible { get; set; }
        public int ParentId { get; set; }
        public MenuDto ParentMenu { get; set; }
        public List<MenuDto> Menus { get; set; }
    }

还有 MenuViewModel

    public class MenuViewModel
    {
        public int Id { get; set; }        
        public bool IsActive { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }
        public string Order { get; set; }
        public string Url { get; set; }
        public bool IsVisible { get; set; }
        public int ParentId { get; set; }
        public MenuViewModel ParentMenu { get; set; }
        public List<MenuViewModel> Menus { get; set; }
    }

这就是我映射 MenuDTO 和 MenuViewModel 对象的方式。

    public class WebProfile : Profile
    {
        public WebProfile()
        {
            CreateMap<MenuDto, MenuViewModel>();
            CreateMap<MenuViewModel, MenuDto>();
        }
    }

我在控制器中这样调用

var navMenuItems = _mapper.Map<List<MenuViewModel>(_menuService.GetNavMenus());

虽然所有字段都已映射,但我在“菜单”字段上收到错误。

我得到的错误信息是;

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
MenuDto -> MenuViewModel
BiPortal2020.Business.ServiceDTOs.Menu.MenuDto -> BiPortal2020.WebUI.Areas.Admin.Models.Menu.MenuViewModel
lambda_method(Closure , MenuDto , MenuViewModel , ResolutionContext )

AutoMapperMappingException: Error mapping types.

Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1

【问题讨论】:

  • BaseDto 中有什么内容?
  • BaseDto 具有字段 Id、DateCreated、DateModified、IsActive。
  • 我认为映射没有任何问题。请在此处更正错字 - _mapper.Map&lt;List&lt;MenuViewModel&gt;(_menuService.GetNavMenus()) 并让我们知道最新的错误消息。
  • 抱歉打错字了,即使该行有 - _mapper.Map&lt;List&lt;MenuViewModel&gt;&gt;(_menuService.GetNavMenus());,我仍然收到同样的错误
  • 你检查过_menuService.GetNavMenus()吗?正在返回List&lt;MenuDto&gt;

标签: asp.net-core-mvc mapping automapper asp.net-core-3.0


【解决方案1】:

错误消息暗示 - AutoMapper 要么无法在 MenuDtoMenuViewModel 之间映射,要么无法找到定义的映射。

我已经测试了您的映射,它们完全没问题。所以,剩下的可能性是 AutoMapper 无法找到您的映射。

我假设您在评论部分提到的Business LayerUI Layer 是两个独立的项目。由于WebProfile 是在UI Layer 中定义的,因此您必须告诉AutoMapper 它应该搜索该程序集以查找映射。由于您在模型和 DTO 之间的映射正常工作,我猜您已经为 BusinessProfile 做了同样的事情,Business Layer 中定义。

我不知道您现有的代码,但您可以这样做 - 在 Startup.Configure 方法中添加/修改以下行 -

services.AddAutoMapper(typeof(IDtoMapping), typeof(IViewModelMapping));

其中IDtoMappingIViewModelMapping分别是Business LayerUI Layer中声明的两个标记接口(空接口,仅用于标识声明它们的程序集)。

【讨论】:

  • 抱歉,回复晚了,我以与您提到的类似的方式解决了问题。感谢您的支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 2019-07-13
  • 1970-01-01
  • 2013-10-19
  • 2021-12-23
相关资源
最近更新 更多