简体   繁体   中英

EF Core encoding with Firebird 2.5

I am migrating a legacy system made in Delphi + FireDac to C# + EF. As it is a legacy system, there is already a database with many records (Many records without encoding information).

When I get records that contain special characters using C# + EF, the characters are not displayed correctly.

How can I fix this problem?

C#

在此处输入图像描述

Delphi

在此处输入图像描述

MainWindow.xaml.cs - ButtonClick

private void Button_Click(object sender, RoutedEventArgs e)
{
    string appPath = AppContext.BaseDirectory;
    string connectionString = $"database=localhost/3050:{appPath}\\database\\test.fdb;user=sysdba;password=masterkey";          
    using (AppDbContext con = new AppDbContext(connectionString))
    {
        Product p = con.Products.First();
        lblText.Text = p.Name;                
    }
}

AppDbContext.cs

public class AppDbContext : DbContext
{
    private readonly string _connectionString;
    public DbSet<Product> Products { get; set; }

    public AppDbContext(string connectionString)
    {
        _connectionString = connectionString;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);

        optionsBuilder.UseFirebird(_connectionString);
    }
}

Product.cs

[Table("PRODUCTS")]
public class Product
{
    [Column("ID")]
    public int Id { get; set; }
    [Column("NAME")]
    public string Name { get; set; }
}

Complete C# project code

For firebird, most of the times, issues with special characters can be handled with the use of CharSet UTF-8. Within connectionString , you may need to include charset=utf8 .

Added update:

Thanks to @Felipe Godinho for the feedback, it seems charset=win1252 has been found to be another valuable option of CharSet that is worth to try when handling legacy database system that is running on Firebird.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM