缺少类型映射配置或不支持 mapping.while 尝试将数据分配给 DTO 对象
Missing type map configuration or unsupported mapping.while trying to assign the data to DTO object
我正在使用 AutoMapper.Extensions.Microsoft.DependencyInjection
。我正在使用 Automapper NuGet 包:AutoMapper.Extensions.Microsoft.DependencyInjection
(7.0.0) for ASP.NET Core 3.1 应用程序。
这是我的域对象:文件ResourceGroup.cs
public class ResourceGroups
{
public string id
{
get;
set;
}
public int ResourceGroupId
{
get;
set;
}
public bool IsPublished
{
get;
set;
}
public int? Position
{
get;
set;
}
public string CreatedBy
{
get;
set;
}
public string CreatedDate
{
get;
set;
}
public string UpdatedBy
{
get;
set;
}
public string UpdatedDate
{
get;
set;
}
public int? ResourceGroupContentId
{
get;
set;
}
public int LanguageId
{
get;
set;
}
public string GroupName
{
get;
set;
}
}
这是我的 DTO 对象:文件 ResourceGroupDTO.cs
public class ResourceGroupDTO
{
public int ResourceGroupId
{
get;
set;
}
public int? Position
{
get;
set;
}
[JsonProperty("groupname")]
[RegularExpression(Constants.GeneralStringRegularExpression)]
public string GroupName
{
get;
set;
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
}
);
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
}
MappingProfile.cs
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<ResourceGroups, ResourceGroupDTO>();
CreateMap<List<ResourceGroups>, List<ResourceGroupDTO>>();
}
}
文件ResourceGroupService.cs
public class ResourceGroupService : IResourceGroupService
{
private readonly DemoDbContext _dbContext;
private readonly ICommonService _commonService;
private readonly IMapper _mapper;
public ResourceGroupService(DemoDbContext dbContext, ICommonService commonService, IMapper mapper)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_commonService = commonService ?? throw new ArgumentNullException(nameof(commonService));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public async Task<ResourceGroupDTO> GetResourceGroupDetailsAsync(int resourceGroupId, int languageId)
{
var resourceGroup = await _dbContext.ResourceGroups.Where(rg => rg.ResourceGroupId.Equals(resourceGroupId) && rg.IsPublished.Equals(true))
.Select(rg => new { rg.ResourceGroupId, rg.Position, rg.GroupName, rg.LanguageId })
.AsNoTracking().ToListAsync();
var resGroup = resourceGroup.FirstOrDefault(rg => rg.LanguageId.Equals(languageId));
return _mapper.Map<ResourceGroupDTO>(resGroup);
}
}
调试上述代码时出现以下错误:
Missing type map configuration or unsupported mapping. \n \nMapping types : \
n<> f__AnonymousType4 ` 4 ->
ResourceGroupDTO \n<> f__AnonymousType4 ` 4
[
[System.Int32, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ,
[System.Nullable ` 1[
[System.Int32, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ] ,
System.Private.CoreLib , Version = 4.0 .0 .0 , Culture = neutral , PublicKeyToken = 7 cec85d7bea7798e ] , [System.String, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ,
[System.Int32, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ] ->
Author.Query.Persistence.DTO.ResourceGroupDTO \n -- ->AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping. \n \nMapping types : \
n<> f__AnonymousType4 ` 4 ->
您正在尝试映射匿名类型,即
new { rg.ResourceGroupId, rg.Position, rg.GroupName, rg.LanguageId }
到 ResourceGroupDTO,因此出现错误。
要快速修复您的错误,您只需将上面的内容更改为
new ResourceGroupDTO { ResourceGroupId = rg.ResourceGroupId, Position = rg.Position, GroupName = rg.GroupName, LanguageId = rg.LanguageId }
然后将 LanguageId 添加到 ResourceGroupDTO 并去掉映射器。
但您真正应该使用的是 ProjectTo,您应该将 .FirstOrDefault 更改为 .Where - 以提高查询效率,格式如下:
await _dbContext.ResourceGroups
.AsNoTracking()
.Where(/* Put your where here */)
.ProjectTo<ResourceGroupDTO>(_mapper.Configuration)
.FirstOrDefaultAsync();
我正在使用 AutoMapper.Extensions.Microsoft.DependencyInjection
。我正在使用 Automapper NuGet 包:AutoMapper.Extensions.Microsoft.DependencyInjection
(7.0.0) for ASP.NET Core 3.1 应用程序。
这是我的域对象:文件ResourceGroup.cs
public class ResourceGroups
{
public string id
{
get;
set;
}
public int ResourceGroupId
{
get;
set;
}
public bool IsPublished
{
get;
set;
}
public int? Position
{
get;
set;
}
public string CreatedBy
{
get;
set;
}
public string CreatedDate
{
get;
set;
}
public string UpdatedBy
{
get;
set;
}
public string UpdatedDate
{
get;
set;
}
public int? ResourceGroupContentId
{
get;
set;
}
public int LanguageId
{
get;
set;
}
public string GroupName
{
get;
set;
}
}
这是我的 DTO 对象:文件 ResourceGroupDTO.cs
public class ResourceGroupDTO
{
public int ResourceGroupId
{
get;
set;
}
public int? Position
{
get;
set;
}
[JsonProperty("groupname")]
[RegularExpression(Constants.GeneralStringRegularExpression)]
public string GroupName
{
get;
set;
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Auto Mapper Configurations
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
}
);
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
}
MappingProfile.cs
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<ResourceGroups, ResourceGroupDTO>();
CreateMap<List<ResourceGroups>, List<ResourceGroupDTO>>();
}
}
文件ResourceGroupService.cs
public class ResourceGroupService : IResourceGroupService
{
private readonly DemoDbContext _dbContext;
private readonly ICommonService _commonService;
private readonly IMapper _mapper;
public ResourceGroupService(DemoDbContext dbContext, ICommonService commonService, IMapper mapper)
{
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
_commonService = commonService ?? throw new ArgumentNullException(nameof(commonService));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public async Task<ResourceGroupDTO> GetResourceGroupDetailsAsync(int resourceGroupId, int languageId)
{
var resourceGroup = await _dbContext.ResourceGroups.Where(rg => rg.ResourceGroupId.Equals(resourceGroupId) && rg.IsPublished.Equals(true))
.Select(rg => new { rg.ResourceGroupId, rg.Position, rg.GroupName, rg.LanguageId })
.AsNoTracking().ToListAsync();
var resGroup = resourceGroup.FirstOrDefault(rg => rg.LanguageId.Equals(languageId));
return _mapper.Map<ResourceGroupDTO>(resGroup);
}
}
调试上述代码时出现以下错误:
Missing type map configuration or unsupported mapping. \n \nMapping types : \
n<> f__AnonymousType4 ` 4 ->
ResourceGroupDTO \n<> f__AnonymousType4 ` 4
[
[System.Int32, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ,
[System.Nullable ` 1[
[System.Int32, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ] ,
System.Private.CoreLib , Version = 4.0 .0 .0 , Culture = neutral , PublicKeyToken = 7 cec85d7bea7798e ] , [System.String, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ,
[System.Int32, System.Private.CoreLib, Version = 4.0 .0 .0 , Culture = neutral, PublicKeyToken = 7 cec85d7bea7798e] ] ->
Author.Query.Persistence.DTO.ResourceGroupDTO \n -- ->AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping. \n \nMapping types : \
n<> f__AnonymousType4 ` 4 ->
您正在尝试映射匿名类型,即
new { rg.ResourceGroupId, rg.Position, rg.GroupName, rg.LanguageId }
到 ResourceGroupDTO,因此出现错误。 要快速修复您的错误,您只需将上面的内容更改为
new ResourceGroupDTO { ResourceGroupId = rg.ResourceGroupId, Position = rg.Position, GroupName = rg.GroupName, LanguageId = rg.LanguageId }
然后将 LanguageId 添加到 ResourceGroupDTO 并去掉映射器。
但您真正应该使用的是 ProjectTo,您应该将 .FirstOrDefault 更改为 .Where - 以提高查询效率,格式如下:
await _dbContext.ResourceGroups
.AsNoTracking()
.Where(/* Put your where here */)
.ProjectTo<ResourceGroupDTO>(_mapper.Configuration)
.FirstOrDefaultAsync();