After hours of trying to figure out the problem, I am still stuck. Here is the problem:
I have a MySql query in my C# code:
var sql = "INSERT INTO MEMBERS(Salutation, FirstName, LastName) VALUES(?salutation, ?firstName, ?lastName)";
This query is in a method which accepts an ExpandoObject:
public async Task AddMember(ExpandoObject expando)
{
var sql = "INSERT INTO MEMBERS(Salutation, FirstName, LastName) VALUES(?salutation, ?firstName, ?lastName)";
// Convert the incoming ExpandoObject to Dapper's DynamicParameters
// https://stackoverflow.com/questions/9481678/how-to-create-arguments-for-a-dapper-query-dynamically
if (expando != null)
{
var dbArgs = new DynamicParameters();
foreach (var param in expando)
{
dbArgs.Add(param.Key, param.Value);
}
await mySqlConnection.ExecuteAsync(sql, dbArgs);
}
}
The query is executing (meaning that I am not getting an error, and a row is created in the Members table, and also the MemberId column value is generated), but Salutation, FirstName and LastName are null.
I checked the contents of the Expando. The input values are all there.
I tried various things.
- Using @ and : instead of ? in the query.
- Using the Expando directly in QueryAsync instead of converting it to DynamicParameters.
- Using an IDictionary as the input instead of the ExpandoObject and passing it directly to QueryAsync.
- Using QueryAsync instead of ExecuteAsync.
Nothing works. The 3 columns are null every time. Not sure what is wrong here.
Due to several reasons, I can't use a strong type like Member as the input to this method. I have to use an ExpandoObject.
- The MySql version I'm using is 5.7.13
- My Dapper version is 1.60.6
- The MySql.Data nuget package that I'm using is 8.0.17