0

I'm attempting to return a List of entities using dapper and a MySQL Stored Procedure, but it returns the data with null values for some reason. It returns the list of entities, but only displays their Id -- CourseName and HoleLayouts are always null. It's a simple query, which is why it is very confusing to me, and it also works for different entities/database tables. I changed the column for course_name to be the type of VARCHAR(255) since originally it was VARCHAR(45) which didn't make a difference. Even with debugging, I can see through the stack trace that the course names are null through the repositories return statement but not their id.

I'll have the code below to try and help further explain.

Entity

public class Course
{
    public int Id { get; set; }
    public string CourseName { get; set; } = null!;
    public ICollection<HoleLayout> HoleLayouts { get; set; } = null!;
}

Repository/Query

    public async Task<IEnumerable<Course>> FindAllAsync()
    { 
        using var connection = _dataAccessor.CreateConnection();

        var courses = await connection.QueryAsync<Course>("GetAllCourses",
            commandType: CommandType.StoredProcedure);

        return courses;
    }

Stored Procedure

create
    definer = root@localhost procedure GetAllCourses()
BEGIN
    SELECT * FROM courses;
END;

JSON Response

[
    {
        "id": 1,
        "courseName": null,
        "holeLayouts": null
    }
]

Course schema

Course Schema

6
  • 1
    I think .ToList(); is needed, var courses = await connection.QueryAsync<Course>("GetAllCourses", commandType: CommandType.StoredProcedure).ToList(); stackoverflow.com/a/41698348 Commented Dec 12, 2022 at 7:19
  • 1
    Can you show the schema of courses? does it have a courseName column? note: Dapper can't help you with holdLayouts - that's complex structured data, which Dapper doesn't do. Also: what data provider is this (in case it is relevant - I simply don't recognize that SQL variant) ? edit: I would expect course_name to work, but again: seeing the schema would help Commented Dec 12, 2022 at 10:13
  • 1
    As a tip: never use * in SQL; if that SP is meant to return the Id, CourseName and something else: say that explicitly - this avoids problems where "safe" changes to the schema (like adding a column) change the way your application works; it also avoids fetching unnecessary data - perhaps a few large text or image columns - over the network, just to be ignored Commented Dec 12, 2022 at 10:15
  • @MarcGravell Thanks Marc, I updated the post to include an image of the schema. It has a course_name column. I'm using MySQL if that helps... I use the same query type to fetch all users and it works fine. Also, regarding the tip to never use *, would using DTOs fix that issue? Commented Dec 12, 2022 at 18:28
  • 1
    @charbs29 no, using DTOs doesn't fix the advice re *; on the same; weird, I would have expected that to work - maybe try adding an underscore in the C#, just to see if that helps? Commented Dec 12, 2022 at 23:06

1 Answer 1

1

As @MarcGravell hinted, it was in fact an issue with the column name in the schema. The solution was for the Entity value name to match the column name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.