litedb-org/LiteDB · error · LiteException

0

0

Error message

Invalid connection string value type for `{key}`

What it means

Thrown by DictionaryExtensions.GetValue<T> (used by the connection string parser) when converting a connection string key's string value to the requested type T fails - TimeSpan parse, enum parse, or Convert.ChangeType all throw, and the generic catch rethrows as this LiteException. It signals a malformed connection string value for a typed setting.

Source

Thrown at LiteDB/Utils/Extensions/DictionaryExtensions.cs:165

                    }
                    else
                    {
                        return (T)(object)TimeSpan.Parse(value);
                    }
                }
                else if (typeof(T).GetTypeInfo().IsEnum)
                {
                    return (T)Enum.Parse(typeof(T), value, true);
                }
                else
                {
                    return (T)Convert.ChangeType(value, typeof(T));
                }
            }
            catch (Exception)
            {
                //TODO: fix string connection parser
                throw new LiteException(0, $"Invalid connection string value type for `{key}`");
            }
        }

        /// <summary>
        /// Get a value from a key converted in file size format: "1gb", "10 mb", "80000"
        /// </summary>
        public static long GetFileSize(this Dictionary<string, string> dict, string key, long defaultValue)
        {
            var size = dict.GetValue<string>(key, null);

            if (size == null) return defaultValue;

            var match = Regex.Match(size, @"^(\d+)\s*([tgmk])?(b|byte|bytes)?$", RegexOptions.IgnoreCase);

            if (!match.Success) return 0;

            var num = Convert.ToInt64(match.Groups[1].Value);

View on GitHub (pinned to f906a5f850)

Solutions

  1. Check the documented type for each connection string key and supply a parseable value.
  2. For numeric sizes use the file-size syntax supported by GetFileSize (e.g. '10MB') where applicable, or plain integers otherwise.
  3. Validate/trim the connection string at config load time; log the offending key.
  4. Remove the problematic key to fall back to its default.

Example fix

// before
var db = new LiteDatabase("Filename=app.db;Timeout=soon"); // throws

// after
var db = new LiteDatabase("Filename=app.db;Timeout=00:01:00");
Defensive patterns

Strategy: try-catch

Validate before calling

static bool TryParseConnValue<T>(string raw, out T value) {
    try { value = (T)Convert.ChangeType(raw, typeof(T)); return true; }
    catch { value = default; return false; }
}
// validate each typed key before constructing LiteDatabase

Try / catch

try { var db = new LiteDatabase(connStr); }
catch (LiteException ex) when (ex.Message.Contains("Invalid connection string value type")) {
    // read ex.Message key, correct the value, rebuild connStr
}

Prevention

When it happens

Trigger: Connection string values that cannot parse to their target type: timeout='abc' (not a TimeSpan/number), a non-numeric value for an int setting (e.g. cache size='big'), an invalid enum name for an enum-typed connection option.

Common situations: Typos in connection string values; copy-pasting a setting name/value between versions where the type changed; locale-specific number formats; passing units where a plain int is expected or vice-versa.

Related errors


AI-assisted analysis of litedb-org/LiteDB@f906a5f850 (2026-08-13). Data as JSON: /api/errors/7a31f4d224d3265b. Report an issue: GitHub.