Unhandled handling When Using Dapper with PostgreSQL JSONB Column in .NET Core 3.1
I've been banging my head against this for hours. I've been researching this but This might be a silly question, but I'm following best practices but I'm working on a personal project and I'm working with an unhandled exception when trying to map a PostgreSQL JSONB column to a C# object using Dapper in my .NET Core 3.1 application. The behavior I receive is: `System.InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'MyNamespace.MyClass'`. My PostgreSQL setup includes a table with a JSONB column, which I'm trying to deserialize into a strong-typed class. Here's the code I'm using for the database query: ```csharp using (var connection = new NpgsqlConnection(connectionString)) { var sql = "SELECT * FROM my_table WHERE id = @Id"; var result = await connection.QuerySingleAsync<MyClass>(sql, new { Id = myId }); } ``` And the class definition for `MyClass` looks like this: ```csharp public class MyClass { public int Id { get; set; } public string Name { get; set; } public Dictionary<string, object> Metadata { get; set; } } ``` I suspect that Dapper is trying to map the JSONB column directly to the `Dictionary<string, object>`, but it's failing due to the type mismatch. I've tried using a custom type handler, but I'm still working with the same scenario. Hereβs the type handler I attempted: ```csharp public class JsonTypeHandler : SqlMapper.TypeHandler<Dictionary<string, object>> { public override void SetValue(IDbDataParameter parameter, Dictionary<string, object> value) { parameter.Value = JsonConvert.SerializeObject(value); } public override Dictionary<string, object> Parse(object value) { return JsonConvert.DeserializeObject<Dictionary<string, object>>(value.ToString()); } } ``` I registered the type handler in Dapper as follows: ```csharp SqlMapper.AddTypeHandler(typeof(Dictionary<string, object>), new JsonTypeHandler()); ``` However, the exception continues. Is there a recommended way to handle JSONB mapping in Dapper for .NET Core 3.1, or am I missing something in my implementation? Any insights would be greatly appreciated! For context: I'm using Csharp on Windows. I'm working on a application that needs to handle this. This is for a application running on Windows 10. I appreciate any insights! I'm on Debian using the latest version of Csharp.