-1

Based on this question's answer: Why asp.net Identity user id is string?

I have made these changes in my project and when i'm creating a migration, it first creates the tables with string based index and then the migration tries to change it's type to int, but it fails and tells me to drop the column and add it again with the new type. When i'm doing this, it gives me a few more errors because the other identity tables are dependent on these id-s, so i should drop the foreign keys first and then drop the column, add the column with new type and then i should re-create the foreign keys.

It requires a lot of effort in my opinion. The above mentioned question is not mentioning this kind of problem. Is there any easy solution for this or do i have to manually write the migration every time when i'm creating a new project and trying to change the identity to work with int based primary key?

1 Answers1

0

I thought that instead of trying to make as little change as possible in the migration, i should simply drop all foreign keys and tables and re-create them with the proper type of Id.

This way i can avoid the errors that are thrown by EF due to changing string to int.

Instead of doing something likes this in the migration:

    migrationBuilder
       .AlterColumn<int>(
            name: "Id",
            table: "AspNetUsers",
            oldClrType: typeof(string));

I do something like this:

        migrationBuilder.DropForeignKey(name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", table: "AspNetRoleClaims");
        // Drop the other FKs as well.

        migrationBuilder.DropTable(name: "AspNetRoles");
        // Drop the other tables as well.

        migrationBuilder.CreateTable(
            name: "AspNetUsers",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false),
                UserName = table.Column<string>(maxLength: 256, nullable: true),
                NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
                Email = table.Column<string>(maxLength: 256, nullable: true),
                NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
                EmailConfirmed = table.Column<bool>(nullable: false),
                PasswordHash = table.Column<string>(nullable: true),
                SecurityStamp = table.Column<string>(nullable: true),
                ConcurrencyStamp = table.Column<string>(nullable: true),
                PhoneNumber = table.Column<string>(nullable: true),
                PhoneNumberConfirmed = table.Column<bool>(nullable: false),
                TwoFactorEnabled = table.Column<bool>(nullable: false),
                LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
                LockoutEnabled = table.Column<bool>(nullable: false),
                AccessFailedCount = table.Column<int>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetUsers", x => x.Id);
            });

        // Create the other tables as well.

Although i'm not sure if there's a more elegant way to replace every UserId, RoleId and Id of AspNetUser, AspNetRole table to int.