I have MySql Tables with autoinc ID columns, such as "director_id" here:
CREATE TABLE directors (
director_id Integer NOT NULL AUTO_INCREMENT,
first_name VarChar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
middle_name VarChar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
last_name VarChar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
suffix VarChar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
PRIMARY KEY (
director_id
)
)
I want to store the autoincremented director_id value in the movies_main Table.
So I try to assign the autoincremented value to an int variable:
long director_id = 0;
...in the call to LastInsertedId here (last line):
if (!alreadyExists)
{
comm.Parameters.Clear();
comm.CommandText = "INSERT INTO Directors " +
"(first_name, middle_name, last_name, suffix) " +
"VALUES " +
"(@first_name, @middle_name, @last_name, @suffix)";
comm.Parameters.AddWithValue("@first_name", directorNamePartsList[0]);
comm.Parameters.AddWithValue("@middle_name", directorNamePartsList[1]);
comm.Parameters.AddWithValue("@last_name", directorNamePartsList[2]);
comm.Parameters.AddWithValue("@suffix", directorNamePartsList[3]);
comm.ExecuteNonQuery();
director_id = comm.LastInsertedId;
}
...and then assign it to the movies_main Table like so:
if (!alreadyExists)
{
comm.Parameters.Clear();
comm.CommandText = "INSERT INTO Movies_Main " +
"(movie_title, mpaa_rating, imdb_rating, movie_length, director_id,
screenwriter_id, year_released) " +
"VALUES " +
"(@movie_title, @mpaa_rating, @imdb_rating, @movie_length, @director_id,
@screenwriter_id, @year_released)";
comm.Parameters.AddWithValue("@movie_title", title);
comm.Parameters.AddWithValue("@mpaa_rating", mpaa_rating);
comm.Parameters.AddWithValue("@imdb_rating", Math.Round(imdb_rating, 1));
comm.Parameters.AddWithValue("@movie_length", movie_length);
comm.Parameters.AddWithValue("@director_id", director_id);
comm.Parameters.AddWithValue("@screenwriter_id", screenwriter_id);
comm.Parameters.AddWithValue("@year_released", year_released);
comm.ExecuteNonQuery();
movie_id = comm.LastInsertedId;
}
Yet the value assigned to the movies_main Table for director_id is always 0!
Why is LastInsertId (apparently) returning 0, and how can I get it to actually return the value its name claims it does? Will I have to resort to a "SELECT MAX(director_id)" query to actually get the value?
NOTE: The movie_id code does work! I get a non-zero value when assigning the result of the call to LastInsertedId to the movie_id variable, and it is added to other tables just fine. This code works as expected:
foreach (var gen_desc in genreList)
{
long genreID = Convert.ToInt32(GetGenreIDForDescription(gen_desc));
alreadyExists = PairAlreadyExistsInMoviesGenresM2Mtable(
movie_id, genreID);
if (!alreadyExists)
{
comm.Parameters.Clear();
comm.CommandText = "INSERT INTO Movies_Genres " +
"(movie_id, genre_id) " +
"VALUES " +
"(@movie_id, @genre_id)";
comm.Parameters.AddWithValue("@movie_id", movie_id);
comm.Parameters.AddWithValue("@genre_id", genreID);
comm.ExecuteNonQuery();
}
}