Why are values from db loaded differently on Console Apps and WebAPIs?

.net-core c# entity-framework-core mysql


I've been using a DbContext (MySql.Data.EntityFrameworkCore 6.10.6) on a WebAPI project (.NET Core 2.0) for quite a while without any problems but now I need to use the same context on a Console Application and special characters aren't being loaded correctly.


public class ProductsController : Controller
    private readonly MyDbContext _context;

    public ProductsController(MyDbContext context)
        _context = context;

    public string Get()
        var productName = _context.Products
            .Single(x => x.Id == 1)

        return productName; = // "PÃO FRANCÊS"

Console App:

private static void Main(string[] args)
    // setup DI

    var context = ServiceProvider.GetService<MyDbContext>();

    var productName = context.Products
        .Single(x => x.Id == 1)

    Console.WriteLine(productName); // "P�O FRANC�S"

Why are these values different even though the same context is used?

Update 1: Changing the Console.OutputEncoding to UTF8 or changing the console font to Lucidas Console or Console didn't change the result. The real purpose of the Console App is to call an API and the result has the � character as soon as I retrieve the value from the DB when debugging. I don't need to print the result on the console.

Update 2: When debugging past the productName variable, the value doesn't change when calling productName or ?productName in the Immediate Window from Visual Studio.

Update 3: I've configured a test database using MS SQL Server and the characters are loaded correctly. It looks like it's a problem with MySql.Data.EntityFrameworkCore

Update 4: Tested it with a new Console App using .NET Framework 4.7.1 and the characters are displayed correctly.

2/22/2018 12:37:05 AM

Accepted Answer

The solution was to add this line to the console app.


Reference: https://github.com/dotnet/corefx/issues/10054

2/13/2018 8:21:24 PM

Popular Answer

Try adding this to get the special characters to load correctly.

Console.OutputEncoding = System.Text.Encoding.Unicode;

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow