问题描述
我正在将用 Delphi + FireDac 制作的遗留系统迁移到 C# + EF.由于是遗留系统,已经有很多记录的数据库(Many records without encoding information).
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).
当我使用 C# + EF 获取包含特殊字符的记录时,字符显示不正确.
When I get records that contain special characters using C# + EF, the characters are not displayed correctly.
我该如何解决这个问题?
How can I fix this problem?
C#
德尔福
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; }
}
推荐答案
对于 firebird,大多数情况下,可以使用 CharSet UTF-8 处理特殊字符问题.在 connectionString
中,您可能需要包含 charset=utf8
.
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
.
添加更新:
感谢@Felipe Godinho 的反馈,似乎 charset=win1252
被发现是 CharSet 的另一个有价值的选项,在处理在 Firebird 上运行的旧数据库系统时值得尝试.
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.
这篇关于使用 Firebird 2.5 进行 EF Core 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!