asp.net-web-api - 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;
}
解决方案
你能在下面的配置服务中注册你的自动映射器并试一试吗?
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
推荐阅读
- sqlite - sqlite 是否支持在 DELETE 子句中加入 JOIN?
- for-loop - 如何处理迭代器的类型?
- javascript - GetElementsByID() 与 QuerySelector()
- html - 检查无线电时如何更改元素的状态?
- instagram-graph-api - 如何在 Instagram Graph API 中获取有限日期范围内的所有帖子?
- firebase - Cloud Firestore 支付
- python - 将包含一些标量和一些数组值的字典写入 csv
- python - 在熊猫数据框中,我想根据将其他列过滤为某些值来为列分配值
- python - 在 Ubuntu 18.04 中安装 Odoo 12
- r - R - 分类变量的一维“热图”