C#C
C#2y ago
nitro

Trying to map values to a single DTO

I have three separate tables, one for the videos, one for all possible genres and one that holds the videoID and genreID so I can tell which video has which genre(s).

Currently it makes sense that I'm getting all possible genres populated instead of only the ones that are actually for the videoID.

How can I solve this properly?

GenreRepository.cs:
public IEnumerable<Genre> getAllGenres()
{
    List<Genre> genres = new List<Genre>();

    using(SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        using(SqlTransaction transaction = connection.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
        {
            using (SqlCommand command = new SqlCommand("SELECT [id],[genre] FROM [dbo].[genres]", connection, transaction))
            {
                using (SqlDataReader reader = command.ExecuteReader())
                {

                    while (reader.Read())
                    {
                        genres.Add(new Genre()
                        {
                            Id = (int)reader["id"],
                            Name = (string)reader["genre"],
                        });
                    }
                }
            }

            transaction.Commit();

        }
    }

    return genres;
}
Was this page helpful?