npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.53k stars 223 forks source link

EFCore + PostgreSQL encoding SQL_ASCII #1169

Closed easoftwaresanderson closed 4 years ago

easoftwaresanderson commented 4 years ago

Hello, I have some clients that using SQL_ASCII and when I try use EFCore with this encoding I get this issues of conversion. Searching for solutions about it , i found this issue in EF with this solution

https://github.com/npgsql/npgsql/commit/10671f03365da8a9cd326eb75e155e06bccc8da1

I need so much of this because we have many clients using this encoding and change for other encondings probaly cause loss of datas.

Is there possible to replicate it to EF Core?

Thanks.

YohDeadfall commented 4 years ago

There is no need to replicate this since the PostgreSQL provider isn't independent and uses the driver under the hood. What you need is to set Encoding property to the required value in your connection string. For example it could be US-ASCII or something else about which the Encoding class knows.

easoftwaresanderson commented 4 years ago

Thanks for you response YohDeadFall

When I Tried this Encoding=SQL_ASCII, in my connection string I get this exception message.

System.ArgumentException: ''SQL_ASCII' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Arg_ParamName_Name'

And when I tried Encoding=US-ASCII I get this exception message. Npgsql.PostgresException: '22021: invalid byte sequence for encoding "UTF8": 0xe7 0x6f 0x73'

I'm using follow packages :

<PackageReference Include="Dapper.FluentMap.Dommel" Version="1.7.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.2.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc5" />

Can anyone help me please ?

YohDeadfall commented 4 years ago

As I know SQL ASCII allows to store anything, so you should know what encoding was used to store data. Probably you could try Portuguese ASCII first since it's your native language.

easoftwaresanderson commented 4 years ago

I understood, The database was created to store data using the SQL_ASCII encoding. In collation i'm using "Portuguese_Brazil.1252". How is the correct way to set my connection string?

I tried this two options : Portuguese_Brazil.1252 and pt-br and get follows exceptions

System.ArgumentException: ''Portuguese_Brazil.1252' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Arg_ParamName_Name'

System.ArgumentException: ''pt-br' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Arg_ParamName_Name'

How can I set correctly this parameter ?

Thanks .

easoftwaresanderson commented 4 years ago

As I know SQL ASCII allows to store anything, so you should know what encoding was used to store data. Probably you could try Portuguese ASCII first since it's your native language.

YohDeadfall,

I solve it using Encoding=LATIN1;Client Encoding=LATIN1

Thanks a lot.

mlaragon commented 2 years ago

El comentario de @easoftwaresanderson me sirvió.

"cnnPostgres": "Host=10.0.1.10;Database=DBTest;Port=5432;User Id=USERA1;Password=User41;Pooling=true;Encoding=LATIN1;Client Encoding=LATIN1;"

Mi base de datos vieja versión Postgres 8.1 está configurada para Enconding = SQL_ASCII en Linux, leyendo desde un proyecto en .Net 6 no permitía leer las tablas ya que generaba error en esa códificación y las tíldes y caracteres latinoamericanos no los leía y producía exception.

gracias por el aporte.