首页 > 解决方案 > AutoMapperMappingException:缺少类型映射配置或不支持的映射。[.NET 核心 3.1]

问题描述

我使用 AutoMapper v10.0 和 AutoMapper Dependency Injection 8.0.1 构建了 WebAPI .NET Core 3.1,但遇到以下错误

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
Role -> RoleResources
Entity.Models.Role -> Entity.Resources.RoleResources
lambda_method(Closure , Role , RoleResources , ResolutionContext )

AutoMapperMappingException: Error mapping types.

Mapping types:
ICollection`1 -> ICollection`1
System.Collections.Generic.ICollection`1[[Entity.Models.Role, Entity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.ICollection`1[[Entity.Resources.RoleResources, Entity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
lambda_method(Closure , ICollection<Role> , ICollection<RoleResources> , ResolutionContext )

我已经厌倦了直接在配置文件中使用 .ReverseMap(),Mapthe ICollection 没有运气。

这是我的课

角色.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Entity.Models
{
    [Table("Role")]
    public class Role
    {
        [Key]
        public Guid Id { get; set; } = Guid.NewGuid();

        [Required]
        public string Name { get; set; }

        [Required]
        public string Description { get; set; }

        [Required]
        public int RoleNumber { get; set; }

        public ICollection<RolePrivilage> RolePrivilages { get; set; }
    }
}

角色资源.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Entity.Resources
{
    public class RoleResources
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public int RoleNumber { get; set; }
    }
}

Startup.cs 配置方法

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<WhiteLandsDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddControllers();

    services.AddAutoMapper(typeof(Startup));

    services.AddScoped<IRoleService, RoleService>();
    services.AddScoped<IPrivilageService, PrivilageService>();

    services.AddScoped<IRoleRepository, RoleRepository>();
    services.AddScoped<IPrivilageRepository, PrivilageRepository>();
}

映射配置文件

using AutoMapper;
using Entity.Models;
using Entity.Resources;
using System.Collections.Generic;

namespace Core.Mapping
{
    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            //CreateMap<ICollection<Role>, ICollection<RoleResources>>();
            CreateMap<Role, RoleResources>();
        }
    }
}

角色控制器.cs

[HttpGet]
public async Task<ICollection<RoleResources>> Get()
{
    var roles = await _roleService.List();
    var resources = _mapper.Map<ICollection<Role>, ICollection<RoleResources>>(roles);
    return resources;
}

标签: asp.net-web-apiautomapperasp.net-core-3.1

解决方案


你能在下面的配置服务中注册你的自动映射器并试一试吗?

   services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

推荐阅读