Skip to content

Microsoft.Data.Sqlite and enum mapping #467

@AdamDotNet

Description

@AdamDotNet

I have a POCO:

public partial class ColumnMap { public long Id { get; set; } public string ColumnName { get; set; } public DataType DataType { get; set; } public long Ordinal { get; set; } public VariableType VariableType { get; set; } }

And some enums:

public enum DataType : long { Byte = 1, Boolean = 2, Int16 = 3, Int32 = 4, Int64 = 5, Single = 6, Double = 7, Decimal = 8, String = 9, Blob = 10 } public enum VariableType : long { Collected = 0, AutoField = 1, Merged = 2, Segment = 3, Weight = 4 }

This is part of my class declaration that uses dapper:

public class DapperSqliteDataHelper : IDataHelper { private bool isInitialized = false; private readonly SqliteConnection connection; private List<ColumnMap> columnMaps; public DapperSqliteDataHelper(SqliteConnection connection) { this.connection = connection; } public async Task AddColumnMappingAsync(string columnName, DataType dataType, VariableType variableType) { await InitializeAsync().ConfigureAwait(false); long nextId = columnMaps.Max(c => c.Id) + 1; long nextOrdinal = columnMaps.Max(c => c.Ordinal) + 1; ColumnMap newMap = new ColumnMap { ColumnName = columnName, DataType = dataType, Id = nextId, Ordinal = nextOrdinal, VariableType = variableType }; await connection.QueryAsync("INSERT INTO column_map (column_id, column_name, ordinal, data_type, variable_type) VALUES (@Id, @ColumnName, @Ordinal, @DataType, @VariableType);", newMap).ConfigureAwait(false); columnMaps.Add(newMap); } //... }

Upon running QueryAsync, the following exception is thrown (I added white space after the @ to avoid tagging people):
Must add values for the following parameters: @ Id, @ ColumnName, @ Ordinal, @ DataType, @ VariableType

at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
at Microsoft.Data.Sqlite.SqliteCommand.d__52.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at Dapper.SqlMapper.<QueryAsync>d__221.MoveNext() in D:\GitHub\dapper-dot-net\Dapper\SqlMapper.Async.cs:line 210

With close inspection in the debugger, I found that after invoking the paramReader (https://github.com/StackExchange/dapper-dot-net/blob/master/Dapper/CommandDefinition.cs#L134), my parameters for enum types are essentially represented as their enum string values instead of long values:
Debugger

The long value should be 9, not "String". Notice the DbType is correct, however.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions