EF 核心迁移错误的拉丁 SQL 编码

ted*_*e24 3 c# entity-framework-core asp.net-core-1.0

我有一个带有 EF7 的 ASP.NET Core 1.0 项目。当我使用 SQL 命令和拉丁字符添加迁移时,我将它们替换为“?” 更新数据库时。

让我们看一个例子:

public partial class TestEncodingMigration : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.Sql(@"some text with latin chars : á é í ñ");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我从那次迁移中得到的脚本:

PM> dnx ef migrations script <some prev migration> -c MyDbContext

some text with accent: ? ? ? ?
;

GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20160216162901_TestEncodingMigration', N'7.0.0-rc1-16348');
Run Code Online (Sandbox Code Playgroud)

如您所见,所有拉丁字符都已替换为“?”

小智 5

我遇到了同样的问题,我通过使用 UTF-8 编码再次保存迁移文件来解决它。

似乎 add-migration 使用 ANSI 编码创建文件。

因此,在记事本中打开迁移 *.cs,然后 File->Save As 并在底部将 Encoding 设置为 UTF-8 然后覆盖。