{"record":{"id":"579b7dc058535453","repo":"tursodatabase/turso","slug":"cannot-convert-value-gettype-value-to-guid","errorCode":null,"errorMessage":"Cannot convert {value.GetType()} value to Guid.","messagePattern":"Cannot convert (.+?) value to Guid\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Data/TursoDataReader.cs","lineNumber":438,"sourceCode":"                 || normalized.Contains(\"CLOB\", StringComparison.Ordinal))\n            fieldType = typeof(string);\n        else if (normalized.Contains(\"BLOB\", StringComparison.Ordinal))\n            fieldType = typeof(byte[]);\n        else\n            return false;\n\n        return true;\n    }\n\n    public static Guid ToGuid(object value)\n    {\n        return value switch\n        {\n            Guid guid => guid,\n            string text => Guid.Parse(text),\n            byte[] bytes when bytes.Length == 16 => new Guid(bytes),\n            byte[] bytes => Guid.Parse(Encoding.UTF8.GetString(bytes)),\n            _ => throw new InvalidCastException($\"Cannot convert {value.GetType()} value to Guid.\")\n        };\n    }\n\n    private static DbType GetDbType(Type fieldType)\n    {\n        if (fieldType == typeof(long))\n            return DbType.Int64;\n        if (fieldType == typeof(double))\n            return DbType.Double;\n        if (fieldType == typeof(string))\n            return DbType.String;\n        if (fieldType == typeof(byte[]))\n            return DbType.Binary;\n        if (fieldType == typeof(Guid))\n            return DbType.Guid;\n\n        return DbType.Object;\n    }","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/tursodatabase/turso/blob/6c7252267988c76e632af00a671e4b9788dfae13/bindings/dotnet/src/Turso.Data/TursoDataReader.cs#L420-L456","documentation":"ToGuid converts a column value to Guid, accepting Guid, string, or 16-byte binary (with a fallback parsing UTF-8 text). Any other runtime type reaches the switch's default arm and throws InvalidCastException, naming the offending type.","triggerScenarios":"Calling reader.GetGuid(ordinal) on a column stored as INTEGER, REAL, or BLOB whose byte length is not 16 — e.g. a UUID stored as TEXT is fine, but one stored as a non-16-byte blob or an integer key is not.","commonSituations":"Reading a UUID column that was written by another client as an integer or 12/20-byte blob; schema drift where the column type changed; mapping a non-UUID column to a Guid property in an ORM.","solutions":["Store UUIDs as TEXT or as exactly 16-byte BLOBs so GetGuid can convert.","Convert in SQL: SELECT CAST(hex_col AS TEXT) or hex(blob_col) and parse on the client.","Read the value as its native type (GetValue/GetInt64) and convert manually.","Check the underlying type with reader.GetFieldType(ordinal) before calling GetGuid."],"exampleFix":"// before\nvar id = reader.GetGuid(0); // column is INTEGER\n// after\nvar id = reader.GetInt64(0);\nvar guid = new Guid(BitConverter.GetBytes(id)); // or fix storage to TEXT/16-byte BLOB","handlingStrategy":"type-guard","validationCode":"var v = reader.GetValue(ordinal);\nbool isGuidLike = v is Guid || (v is string) || (v is byte[] b && b.Length == 16);","typeGuard":"static bool CanReadAsGuid(object? value) => value is Guid or string or byte[] { Length: 16 };","tryCatchPattern":"try { guid = reader.GetGuid(ordinal); }\ncatch (InvalidCastException ex) when (ex.Message.EndsWith(\"value to Guid.\"))\n{ guid = Guid.Parse(reader.GetValue(ordinal).ToString()!); }","preventionTips":["Store UUIDs as TEXT or exactly 16-byte BLOBs.","CAST in SQL before reading when the storage type is uncertain.","Verify column types with GetFieldType before typed Get* calls."],"tags":["csharp","dotnet","datareader","cast","guid"],"backgroundTag":"type-mismatch","analyzedSha":"6c7252267988c76e632af00a671e4b9788dfae13","analyzedAt":"2026-09-06T07:15:26.990Z","contentChangedAt":"2026-09-06T07:15:26.990Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}