.NET控制器GetAll/GetById接口未返回Full_name字段排查求助
问题描述
数据库中AbilityScore记录的Full_name字段存在有效值,但调用GetAll或GetById接口时该字段未被返回,请问遗漏了什么?
共享项目
模型(Model)
public class AbilityScore : BaseEntity { public string Index { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; public string Full_name { get; set; } = string.Empty; public string Desc { get; set; } = string.Empty; public string Url { get; set; } = string.Empty; }
数据传输对象(DTOs)
public record struct AbilityScoreResponse( int Id, string Index, string Name, string Full_name, string Desc, string Url );
服务端项目
AbilityScore仓储(AbilityScoreRepositories)
public interface IAbilityScoreRepository { Task<List<AbilityScore>> Create(AbilityScore abilityScore); Task<List<AbilityScore>?> HardDeleteById(int id); Task<List<AbilityScore>?> GetAll(); Task<AbilityScore?> GetById(int id); Task<List<AbilityScore>> Update(int id, AbilityScore abilityScore); } public class AbilityScoreRepository : IAbilityScoreRepository { private readonly DataContext _dataContext; public AbilityScoreRepository(DataContext dataContext) { _dataContext = dataContext; } public async Task<List<AbilityScore>> Create(AbilityScore abilityScore) { _dataContext.AbilityScores.Add(abilityScore); await _dataContext.SaveChangesAsync(); return await GetAll(); } public async Task<List<AbilityScore>?> GetAll() { return await _dataContext.AbilityScores.ToListAsync(); } public async Task<AbilityScore?> GetById(int id) { var dbObject = await _dataContext.AbilityScores.FirstOrDefaultAsync(x => x.Id == id); return dbObject; } public async Task<List<AbilityScore>?> HardDeleteById(int id) { var dbObject = await _dataContext.AbilityScores.FirstOrDefaultAsync(x => x.Id == id); if (dbObject is null) { return null; } _dataContext.AbilityScores.Remove(dbObject); await _dataContext.SaveChangesAsync(); return await GetAll(); } public async Task<List<AbilityScore>> Update(int id, AbilityScore abilityScore) { var dbObject = await _dataContext.AbilityScores.FirstOrDefaultAsync(x => x.Id == id); if ((dbObject is null)) { throw new EntityNotFoundException($"Ability Score with ID {id} was not found."); } dbObject.Index = abilityScore.Index; dbObject.Name = abilityScore.Name; dbObject.Full_name = abilityScore.Full_name; dbObject.Desc = abilityScore.Desc; dbObject.Url = abilityScore.Url; await _dataContext.SaveChangesAsync(); return await GetAll(); } }
AbilityScore服务(AbilityScoreServices)
public interface IAbilityScoreService { Task<List<AbilityScoreResponse>> Create(AbilityScoreCreateRequest request); Task<List<AbilityScoreResponse>?> HardDelete(int id); Task<List<AbilityScoreResponse>> GetAll(); Task<AbilityScoreResponse?> GetById(int id); Task<List<AbilityScoreResponse>?> Update(int id, AbilityScoreUpdateRequest request); } public class AbilityScoreService : IAbilityScoreService { private readonly IAbilityScoreRepository _abilityScoreRepository; public AbilityScoreService(IAbilityScoreRepository abilityScoreRepository) { _abilityScoreRepository = abilityScoreRepository; } public async Task<List<AbilityScoreResponse>> GetAll() { var result = await _abilityScoreRepository.GetAll(); return result.Adapt<List<AbilityScoreResponse>>(); } }
控制器(Controller)
public class AbilityScoresController : ControllerBase { private readonly IAbilityScoreService _abilityScoreService; public AbilityScoresController(IAbilityScoreService abilityScoreService) { _abilityScoreService = abilityScoreService; } [HttpGet] public async Task<ActionResult<List<AbilityScoreResponse>>> GetAll() { return Ok(await _abilityScoreService.GetAll()); } [HttpGet("{id}")] public async Task<ActionResult<AbilityScoreResponse>> GetById(int id) { var dbObject = await _abilityScoreService.GetById(id); if (dbObject is null) { return NotFound($"Ability Score with the given ID {id} was not found."); } return Ok(dbObject); } }
排查方向及解决方法
AutoMapper 映射配置缺失
服务层使用的Adapt方法(推测为AutoMapper扩展)未配置AbilityScore到AbilityScoreResponse的映射规则。即使字段同名,下划线命名可能需要显式配置才能正确映射。- 解决:添加映射配置:
CreateMap<AbilityScore, AbilityScoreResponse>();
- 解决:添加映射配置:
GetById方法未实现映射逻辑
提供的AbilityScoreService代码中仅实现了GetAll的映射,GetById方法的实现缺失,导致数据库实体未转换为DTO,字段无法正确返回。- 解决:补全
GetById方法:public async Task<AbilityScoreResponse?> GetById(int id) { var result = await _abilityScoreRepository.GetById(id); return result?.Adapt<AbilityScoreResponse>(); }
- 解决:补全
JSON序列化字段名称不匹配
ASP.NET Core默认会将Full_name序列化为驼峰式fullName,若客户端期望原字段名,会产生“字段未返回”的错觉;也可能存在[JsonIgnore]属性误加的情况。- 解决:检查字段是否有
[JsonIgnore],或配置序列化保留原字段名:builder.Services.AddControllers() .AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = null; });
- 解决:检查字段是否有
EF Core 模型映射问题
若DataContext中未正确映射Full_name字段到数据库列,或查询时该字段未被追踪,会导致实体中字段为空。- 解决:检查
OnModelCreating中的配置:modelBuilder.Entity<AbilityScore>() .Property(a => a.Full_name) .HasColumnName("Full_name"); // 确保与数据库列名匹配
- 解决:检查
内容的提问来源于stack exchange,提问作者Darkmatter5
相关产品推荐
相关产品推荐

