EF6 + PostGresql - 22021: invalid byte sequence for encoding \ "UTF8 \": 0xc9 0x52


I am trying to get data from a PostGreSql database with SQL_ASCII Enconding, however in some records I get the error message 22021: invalid byte sequence for encoding \ "UTF8 \": 0xc9 0x52 " / p>

I have tried to set the Enconding in the connection string in several ways, but without success, the error occurs "Keyword not supported"

Does anyone know of a solution other than swapping Enconding from bank to UTF8?

asked by anonymous 04.06.2016 / 22:10

2 answers


Bad news. The Npgsql team has given up support for SQL_ASCII .

Directly forcing LATIN1 to string s, this user to use SQL_ASCII on his system . The problem is that this can hardly be used together with the Entity Framework, whose parameterization activity is automatic.

I see no alternatives other than bank migration to UTF-8, which is highly recommended.

04.06.2016 / 23:59

In version 3.1.8 added support for ClientEncoding in the connection string, which solves many problems: I already answered it here: #

The solution is:

var sqlBuilder = new NpgsqlConnectionStringBuilder
                    Host = strLocal,
                    Database = strNome,
                    Username = strUser,
                    Password = strSenha,
                    Pooling = false,
                    Encoding = "windows-1252",
                    ClientEncoding = "sql-ascii"

 string strConexao = sqlBuilder.ConnectionString;

Version log: link

Issue Log: link

04.11.2016 / 14:36