[Solved] How to insert byte[] array with ORMlite into image column


In ServiceStack.OrmLite v4, as specified by mythz in the comments, everything should work as-is.


In v3, if you want to have byte arrays serialized as binary instead of base 64 strings, you should inherit from SqliteOrmLiteDialectProvider and override the functions responsible for converting CLR values from and to SQL values to handle byte arrays.

public class ByteArrayAwareSqliteOrmLiteDialectProvider
    : SqliteOrmLiteDialectProvider {

    public override object ConvertDbValue(object value, Type type) {
        var bytes = value as byte[];
        if (bytes != null && type == typeof(byte[]))
            return bytes;

        return base.ConvertDbValue(value, type);
    }

    public override string GetQuotedValue(object value, Type fieldType) {
        var bytes = value as byte[];
        if (bytes != null && fieldType == typeof(byte[]))
            return "X'" + BitConverter.ToString(data).Replace("-", "") + "'";

        return base.GetQuotedValue(value, fieldType);
    }

}

Then, configure your dialect provider properly:

OrmLiteConfig.DialectProvider = new ByteArrayAwareSqliteOrmLiteDialectProvider();

4

solved How to insert byte[] array with ORMlite into image column