【发布时间】:2021-11-17 01:23:57
【问题描述】:
我目前正在使用 .NET Web Api 来获取基于国家/地区的县列表。 在尝试获取县时,我收到“Automapper:缺少类型映射配置或不支持的映射”异常。
我的域类是:
public class County
{
public string Id { get; set; }
public string Name { get; set; }
public string CountryId { get; set; }
public virtual Country Country { get; set; }
public virtual IList<City> City { get; set; }
}
我的 DTO 是
public class CountyResponse
{
public string Id { get; set; }
public string Name { get; set; }
public string CountryId { get; set; }
}
映射配置文件:
public class CountyProfile : Profile
{
public CountyProfile()
{
CreateMap<CountyResponse, County>().ReverseMap();
}
}
我的服务类 GetAllLMethod:
public async Task<Result<List<CountyResponse>>> GetAllAsync()
{
var counties = _context.Counties.ToListAsync();
var mappedCounties = _mapper.Map<List<CountyResponse>>(counties);
return await Result<List<CountyResponse>>.SuccessAsync(mappedCounties);
}
构建正常,并且我的 Country 域映射完全相同 (1:1)。 然而,一个有效,而这个,在尝试访问 Get Endpoint 时,我收到了这个错误。 知道这里有什么问题吗? (服务已注册 - 我正在使用以下内容注入它们:
public static void AddInfrastructureMappings(this IServiceCollection services)
{
services.AddAutoMapper(Assembly.GetExecutingAssembly());
}
【问题讨论】:
标签: c# .net api mapping automapper