0

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

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Alzack
  • 1

1 Answers1

0

Thank you all for your responses. Unfortunately with my architecture I could not apply any of the solutions. I finally solved the problem by changing the field type in the database from enum to string. I do the validation of the enum correctness on the C# side.

For my use, the problem solved. If anyone would like to solve this problem "as it should be" I think the problem lies on the Dapper side.

Best Regards!

Alzack
  • 1