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

标签 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)
    {
    }
}

这是我从该迁移中获得的脚本:

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');

如您所见,所有的拉丁字符都被替换成了“?”

最佳答案

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

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

所以,在记事本中打开migration *.cs,然后File->Save As,在底部设置Encoding为UTF-8,然后覆盖。

关于c# - EF 核心迁移错误的拉丁 SQL 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35438673/

相关文章:

c# - 这两个使用 foreach 的 IEnumerable<T> 代码之间会有性能差异吗?

cors - "The call is ambiguous between the following methods"服务.AddCors()

asp.net-core - 自定义内容类型验证过滤器?

asp.net-mvc - 发布 List<Interface> .net core 1.0

c# - 这在 LINQ 中可能吗?

javascript - 如何在 ASP.net (.aspx) 中 5 分钟后显示按钮?

c# - 为 EF 6.x 和 EF Core 重用 EF 实体

c# - EF Core 的 HasColumnName 发生了什么变化?

c# - 通过 C# 代码解析/重构 C#

c# - 在任何 EntityCore 中使用表达式树