efcore: Specify CultureInfo in Controller make query not working

I want to force the user UI to be in Thai language. So, I set cultureinfo using this code.

var cultureInfo = CultureInfo.GetCultureInfo("th-TH");
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

I also tried app.UseRequestLocalization and added only “th-TH” supported. It’s working fine on windows. But not in linux. I have tried running on asp.net core running docker image from mcr.microsoft.com. Still, it didn’t work. I also tried on CentOS within and outside the container. It produces the same issue.

After adding this code, on Windows, the date time is rendered in Thai format properly. When running kestrel on linux and the query with parameter (Where clause) is specified in LINQ, it throws an exception.

PostgresException: 42703: column “__normalizedusername_0” does not exist

Steps to reproduce

Add this code.

var cultureInfo = CultureInfo.GetCultureInfo("th-TH");
Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

and try running

await _userManager.FindByNameAsync(username_to_test);

Output on /Identity/Account/Login

Npgsql.NpgsqlConnector+<>c__DisplayClass160_0+<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
Npgsql.NpgsqlConnector+<>c__DisplayClass160_0+<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
Npgsql.NpgsqlDataReader.NextResult(bool async, bool isConsuming)
Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, bool async, CancellationToken cancellationToken)
Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor+AsyncQueryingEnumerable<T>+AsyncEnumerator.MoveNextAsync()
System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>.GetResult()
Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource> asyncEnumerable, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync<TSource>(IAsyncEnumerable<TSource> asyncEnumerable, CancellationToken cancellationToken)
Microsoft.AspNetCore.Identity.UserManager<TUser>.FindByNameAsync(string userName)
Microsoft.AspNetCore.Identity.SignInManager<TUser>.PasswordSignInAsync(string userName, string password, bool isPersistent, bool lockoutOnFailure)
eSignatureWebApp.Areas.Identity.Pages.Account.LoginModel.OnPostAsync(string returnUrl) in Login.cshtml.cs

Further technical details

EF Core version: 3.0.0 Database provider: Npgsql 4.1.1 and Npgsql.EntityframeworkCore.Postgresql 3.0.1 Target framework: .net core 3.0 Operating system: CentOS 7 within container, CentOS 7, container mcr.microsoft.com/dotnet/core/aspnet:3.0 IDE: Visual Studio 2019

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I have implemented suggested work-around and it appears to work

Will show solution here if anyone else needs it. But I do believe this change would be nice to get into mainline, seems really trivial fix.

public class OrdinalCompareNpgsqlGenerationHelper : NpgsqlSqlGenerationHelper
{
    public OrdinalCompareNpgsqlGenerationHelper(RelationalSqlGenerationHelperDependencies dependencies) 
         : base(dependencies)
    {
    }

    public override string GenerateParameterName(string name)
        => name.StartsWith("@", StringComparison.Ordinal)
            ? name
            : "@" + name;
}
var dbContextOption = new DbContextOptionsBuilder<T>()
    .UseNpgsql(dbConnectionOptions.ConnectionString)
    .ReplaceService<ISqlGenerationHelper, OrdinalCompareNpgsqlGenerationHelper>()

When 3.1.11 that contains this fix will be released?

Tomorrow (January 12th)