skip to Main Content

I’m going to try my best to include everything you’ll need to help me diagnose my issue. The database record includes a value in the Full_name field, but when I run the GetAll or GetById it isn’t returned. What am I missing? Thank you in advance!

Shared Project

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
);

Server Project

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

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);
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    I changed the field to Fullname and it's working now. Thank you!


  2. Debugging first:
    I think you should start by adding some logs in between these lines,

    dbObject.Index = abilityScore.Index;
        dbObject.Name = abilityScore.Name;
        dbObject.Full_name = abilityScore.Full_name;
        dbObject.Desc = abilityScore.Desc;
        dbObject.Url = abilityScore.Url;
        //here print dboobject, and abilitScore object
        await _dataContext.SaveChangesAsync();
        return await GetAll();
    

    then see what is not being correctly assigned, your dbObject.full_name should be an empty string correct? but your abilitScore.full_name shouldn’t. I have a feeling that your _ is causing you some issues.
    Let me know what happens!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search