{"record":{"id":"3c85a2ddb2b35253","repo":"tursodatabase/turso","slug":"invalid-value-value-for-enum-type-enumtype","errorCode":null,"errorMessage":"Invalid value {value} for enum type {enumType}.","messagePattern":"Invalid value (.+?) for enum type (.+?)\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnectionStringBuilder.cs","lineNumber":270,"sourceCode":"    }\n\n    private int GetInt(string keyword, int defaultValue)\n    {\n        return base.TryGetValue(keyword, out var value)\n            ? Convert.ToInt32(value, CultureInfo.InvariantCulture)\n            : defaultValue;\n    }\n\n    private TEnum GetEnum<TEnum>(string keyword, TEnum defaultValue)\n        where TEnum : struct\n    {\n        if (!base.TryGetValue(keyword, out var value))\n            return defaultValue;\n\n        if (value is TEnum typedValue)\n        {\n            if (!Enum.IsDefined(typeof(TEnum), typedValue))\n                throw new ArgumentOutOfRangeException(nameof(value), value, Properties.Resources.InvalidEnumValue(typeof(TEnum), typedValue));\n\n            return typedValue;\n        }\n\n        if (value is string stringValue && Enum.TryParse<TEnum>(stringValue, ignoreCase: true, out var parsedValue))\n            return parsedValue;\n\n        return (TEnum)Enum.ToObject(typeof(TEnum), Convert.ToInt32(value, CultureInfo.InvariantCulture));\n    }\n\n    private void SetNullable<T>(string keyword, T? value)\n        where T : struct\n    {\n        if (value.HasValue)\n            this[keyword] = value.Value;\n        else\n            Remove(keyword);\n    }","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/dotnet/src/Turso.Data.Sqlite/SqliteConnectionStringBuilder.cs#L252-L288","documentation":"ArgumentOutOfRangeException thrown by GetEnum<TEnum> when reading an enum-typed option (Mode, Cache, DateTimeKind): the stored value is already a TEnum instance but its numeric value is not defined on the enum. This only happens when an undefined enum value was placed into the underlying store through a path that bypassed the setter's converter, for example direct DbConnectionStringBuilder access or reflection. Normal string parsing in this getter deliberately skips the IsDefined check, so the typed-value branch is the guarded one.","triggerScenarios":"Storing an unvalidated boxed enum such as ((DbConnectionStringBuilder)builder)[\"Mode\"] = (SqliteOpenMode)99 (or via reflection) and then reading builder.Mode; interop code that injects raw object values into the keyword table; values persisted from a version that defined different enum members.","commonSituations":"Configuration systems that write raw objects into DbConnectionStringBuilder; deserializing settings dictionaries into the builder without conversion; version drift where a stored numeric enum value no longer maps to a defined member.","solutions":["Set enum options through the strongly-typed properties (builder.Mode = SqliteOpenMode.ReadOnly) so values are converted and validated on write.","If you must write raw objects, validate first with Enum.IsDefined(typeof(SqliteOpenMode), value) before storing.","Reset the suspect keyword (builder.Remove(\"Mode\")) so the getter returns its default instead of the poisoned stored value."],"exampleFix":"// before\n((DbConnectionStringBuilder)builder)[\"Mode\"] = (SqliteOpenMode)99;\nvar mode = builder.Mode; // throws: 99 not defined\n\n// after\nbuilder.Mode = SqliteOpenMode.ReadWriteCreate; // validated on write","handlingStrategy":"validation","validationCode":"if (rawValue is SqliteOpenMode m && !Enum.IsDefined(m))\n    throw new ConfigException($\"Mode value {m} is not defined.\");\nbuilder.Mode = (SqliteOpenMode)Enum.ToObject(typeof(SqliteOpenMode), Enum.IsDefined(m) ? m : SqliteOpenMode.ReadWriteCreate);","typeGuard":"static bool IsValidEnum<TEnum>(object? v) where TEnum : struct, Enum\n    => v is TEnum e && Enum.IsDefined(e);","tryCatchPattern":"try { var mode = builder.Mode; }\ncatch (ArgumentOutOfRangeException) { builder.Remove(\"Mode\"); mode = SqliteOpenMode.ReadWriteCreate; }","preventionTips":["Write enum options through typed properties so values are validated at set time.","Never inject raw boxed enums into the underlying DbConnectionStringBuilder store."],"tags":["csharp","dotnet","connection-string","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}