I have problem with inserting record with enum to database using Blazor framework.
**My model looks like this: **
public class Customer
{
public int Id { get; set; } //primary, auto-increment
public string CompanyName { get; set; }
public string VATNum { get; set; }
public CustomerGroup CustomerGroup { get; set; }
}
Enum
public enum CustomerGroup
{
National,
Foreign
}
Method with sql and db query call
public Task InsertCustomer(Customer customer)
{
string sql = @"INSERT INTO Customer (CompanyName, VATNum, CustomerGroup) VALUES (@CompanyName, @VATNum, @CustomerGroup)";
return _db.SaveData(sql, customer);
}
SaveData method
public async Task SaveData<T>(string sql, T parameters)
{
using (IDbConnection connection = new MySqlConnection(_config.GetConnectionString(ConnectionStringName)))
{
await connection.ExecuteAsync(sql, parameters);
}
}
I think the problem is that CustomerGroup is of type enum, not string, and blazor can't convert it. When I hardcoded in sql variable, @CustomerGroup to 'National', everything works fine.
Best regards!
I tried to add [JsonConverter(typeof(StringEnumConverter))] to enum declaration Also tried to wrap field in model in [Column(TypeName= "nvarchar(10)")]
Nothing has helped