AutoMapperMappingException:缺少类型映射配置或不支持的映射。 [.NET Core 3.1]

时间:2020-08-10 10:42:46

标签: asp.net-web-api automapper asp.net-core-3.1

我已经使用AutoMapper v10.0和AutoMapper依赖项注入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。

这是我的课程

Role.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; }
    }
}

RoleResources.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>();
        }
    }
}

RoleController.cs

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

1 个答案:

答案 0 :(得分:0)

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

   services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
相关问题