Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The object 'PK_AspNetUserTokens' is dependent on column 'Name'. ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column

I am trying to extend the IdentityUser class. I added a new class ApplicationUser and inherit IdentityUser class. Migration is added successfully but on updating database, I am getting the error "The object 'PK_AspNetUserTokens' is dependent on column 'Name'.ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column.". I opened the SSMS and looked for data in AspNetUserToken, the table was empty.

a couple of things I have tried but end up with the same error. I replaced all the references to the IdentityUser class in my code. Delete the data in table 'AspNetUsers'. Remove the migration after replacing references and deleting data. Again added migration and update database, the error was still there.

AppDbContext.cs


namespace PieShop.Data_Access_Layer
{
    public class AppDbContext :IdentityDbContext<ApplicationUser>
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
            :base(options)
        {
        }
        public DbSet<Pie> Pies { get; set; }
        public DbSet<Feedback> Feedbacks { get; set; }
    }
}


IdentityHostingStartup.cs


[assembly: HostingStartup(typeof(PieShop.Areas.Identity.IdentityHostingStartup))]
namespace PieShop.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<AppDbContext>();
            });
        }
    }
}
ApplicationUser.cs


namespace PieShop.Models
{
    public class ApplicationUser : IdentityUser
    {
        [Required]
        [MaxLength(30)]
        public string City { get; set; }
        [Required]
        public string Address  { get; set; }
        [Required]
        [MaxLength(20)]
        public string Country { get; set; }
    }
}

Migration

using Microsoft.EntityFrameworkCore.Migrations;

namespace PieShop.Migrations
{
    public partial class ApplicationUserAdded : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
        migrationBuilder.AlterColumn<string>(
            name: "Name",
            table: "AspNetUserTokens",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string));

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserTokens",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string));

        migrationBuilder.AddColumn<string>(
            name: "Address",
            table: "AspNetUsers",
            nullable: false,
            defaultValue: "");

        migrationBuilder.AddColumn<string>(
            name: "City",
            table: "AspNetUsers",
            maxLength: 30,
            nullable: false,
            defaultValue: "");

        migrationBuilder.AddColumn<string>(
            name: "Country",
            table: "AspNetUsers",
            maxLength: 20,
            nullable: false,
            defaultValue: "");

        migrationBuilder.AlterColumn<string>(
            name: "ProviderKey",
            table: "AspNetUserLogins",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string));

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserLogins",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string));
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "Address",
            table: "AspNetUsers");

        migrationBuilder.DropColumn(
            name: "City",
            table: "AspNetUsers");

        migrationBuilder.DropColumn(
            name: "Country",
            table: "AspNetUsers");

        migrationBuilder.AlterColumn<string>(
            name: "Name",
            table: "AspNetUserTokens",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserTokens",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);

        migrationBuilder.AlterColumn<string>(
            name: "ProviderKey",
            table: "AspNetUserLogins",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserLogins",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);
    }
}

}

like image 442
Saad Sid Avatar asked Jul 27 '19 23:07

Saad Sid


1 Answers

I solved it by editing the migration and adding drop and add commands for the Primary Key.

At the top of the new migration, add:

migrationBuilder.DropPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens");

and after all modifications to AspNetUserTokens have taken place, add

migrationBuilder.AddPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens", new[] { "UserId", "LoginProvider", "Name" });
like image 196
Sergi Papaseit Avatar answered Sep 25 '22 13:09

Sergi Papaseit