表“AspNetRoles”中的列“Id”的类型无法用作索引中的键列

Kee*_*ing 3 entity-framework entity-framework-5 asp.net-identity

我使用的是.net5.0 EF

我有一个 AppUser 类并将其扩展为 IdentityUser

public class AppUser : IdentityUser
{
}
Run Code Online (Sandbox Code Playgroud)

我正在使用命令生成 EF 迁移 dotnet ef migrations add "myMessage" -p Persistence -s API

我已经删除了所有以前的迁移文件,所以这是一个新的迁移。

我也删除了数据库。

我能够在 sqlLite 中成功生成新数据库。

但是当我尝试在生产/测试服务器 SQL 中执行相同操作时,它给了我以下问题。

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
      Failed executing DbCommand (38ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
      CREATE TABLE [AspNetRoles] (
          [Id] TEXT NOT NULL,
          [Name] TEXT NULL,
          [NormalizedName] TEXT NULL,
          [ConcurrencyStamp] TEXT NULL,
          CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
      );
fail: API.Program[0]
      An error occured during migration
      Microsoft.Data.SqlClient.SqlException (0x80131904): Column 'Id' in table 'AspNetRoles' is of a type that is invalid for use as a key column in an index.
      Could not create constraint or index. See previous errors.
         at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
         at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
         at Microsoft.Data.SqlClient.SqlCommand.InternalEndExecuteNonQuery(IAsyncResult asyncResult, Boolean isInternal, String endMethod)
         at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
         at Microsoft.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)
         at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
Run Code Online (Sandbox Code Playgroud)

该错误与表中的某些内容有关AspNetRoles但我什至没有触及该AspNetRoles表中的任何内容。

有人可以帮我吗?

谢谢


下面自动生成的迁移代码

protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "AspNetRoles",
                columns: table => new
                {
                    Id = table.Column<string>(type: "TEXT", nullable: false),
                    Name = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
                    NormalizedName = table.Column<string>(type: "TEXT", maxLength: 256, nullable: true),
                    ConcurrencyStamp = table.Column<string>(type: "TEXT", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AspNetRoles", x => x.Id);
                });
Run Code Online (Sandbox Code Playgroud)

ram*_*hin 5

为 SQLite 创建的迁移不可用于 Sql Server,因此您必须像这样创建新的迁移:

步骤1:将“UseSqlite”更改为“UseSqlServer”

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlite(connectionString));
Run Code Online (Sandbox Code Playgroud)

builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
Run Code Online (Sandbox Code Playgroud)

步骤 2:删除所有迁移(通过删除“Migrations”文件夹)

步骤3:添加新的迁移,然后更新数据库

dotnet ef migrations add "CreateIdentitySchema"
dotnet ef database update
Run Code Online (Sandbox Code Playgroud)