Cannot map PostgreSQL text[] type to List<string>
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
I have a PostgreSQL table named foo with a bar column of type text[]. I try to execute the following code:
public class Foo
{
public List<string> Bar { get; set; }
}
public async Task<Foo> GetById(Guid id)
{
var param = new { Id = id };
string sql = "SELECT bar AS Bar FROM foo WHERE id = @Id";
return await this._connection.QuerySingleOrDefaultAsync<Foo>(sql, param);
}
The QuerySingleOrDefaultAsync line fails with the following exception:
System.Data.DataException : Error parsing column 2 (bar=System.String[] - Object)
----> System.InvalidCastException : Unable to cast object of type 'System.String[]' to type 'System.Collections.Generic.List`1[System.String]'.
The code works if I change the Foo.Bar property to string[], but I find the behavior inconsistent because Npgsql is supposed to support both regular .NET arrays and List<> types when mapping to/from a PostgreSQL array. Furthermore, performing an INSERT with a List<> parameter does work (supposedly because Dapper mapping doesn't intervene in this case).
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the QuerySingleOrDefaultAsync mapping against a PostgreSQL text[] column, then inspect Dapper's result-column conversion path for List. Done means Foo.Bar maps to List without the InvalidCastException while existing string[] mapping continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, postgresql
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100