使用迁移脚本更改 EF Core 中的数据库排序规则

问题描述 投票:0回答:1

我正在尝试使用迁移脚本更改数据库的排序规则。

这是我尝试使用 EF Core 更新排序规则的代码。

protected override void Up(MigrationBuilder migrationBuilder)
{
        _ = migrationBuilder.AlterDatabase("SQL_Latin1_General_CP1_CS_AS");

        _ = migrationBuilder.CreateTable(
            name: "Employee",
            columns: table => new
            {
                Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                EmployeeName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false, collation: "LATIN1_GENERAL_CI_AI"),
            });
}

我收到此错误:

重置连接会导致与初始登录不同的状态。登录失败。
用户“sa”登录失败。
无法继续执行,因为会话处于终止状态。
当前命令发生严重错误。结果,如果有的话,应该被丢弃

c# sql-server .net-core entity-framework-core collation
1个回答
0
投票

答案
出现错误是因为连接已重置并且连接处于终止状态,如消息所示。

解决方案
为每个特定更改创建单独的迁移,而不是将多个更改合并到单个迁移中。

根据您的情况,将迁移脚本分为两个脚本,如下所示:

迁移脚本1:

// Change DB Collation:
protected override void Up(MigrationBuilder migrationBuilder)
{
        migrationBuilder.AlterDatabase("SQL_Latin1_General_CP1_CS_AS");
}

迁移脚本2:

// Create Table ...
protected override void Up(MigrationBuilder migrationBuilder)
{
        migrationBuilder.CreateTable(
            name: "Employee",
            columns: table => new
            {
                Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                EmployeeName = table.Column<string>(type: "nvarchar(100)", maxLength: 100, nullable: false, collation: "LATIN1_GENERAL_CI_AI"),
            });
}
© www.soinside.com 2019 - 2024. All rights reserved.