Search code examples
entity-framework-coreasp.net-core-mvcasp.net-identity

There is already an object named 'AspNetRoles' in the database. Trying to add migration


I've read similar threads and they suggest deleting migrations folder, and deleting database. The problem is, this is a production database. Its hard to believe you have to delete and repopulate a production database to make a change.

So I have an app, Asp.Net MVC targeting Core 3.1, called CompanyPortal.Web I also have a class library called CompanyIdentity.DAL

Identity is setup in the dll.

I've extended IdentityUser:

namespace CompanyIdentity.DAL.Data
{
    public class CompanyPortalUser: IdentityUser
    {
        public string CoreUserId { get; set; }
    }
}

There is only one migration so far, the initial one: 20200711193329_InitialDbCreation.cs

I am trying to change the data type of CoreUserId and add a couple fields:

namespace CompanyIdentity.DAL.Data
{
    public class CompanyPortalUser: IdentityUser
    {
        public int CoreUserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

I get the error when I use the command: Update-Database

I believe this is all the code related to this:

namespace CompanyIdentity.DAL.Data
{
    public class CompanyIdentityDbContext: IdentityDbContext<CompanyPortalUser>
    {
        public CompanyIdentityDbContext(DbContextOptions<CompanyIdentityDbContext> options): base(options)
        {

        }
    }
}
namespace CompanyIdentity.DAL.Migrations
{
    public partial class AddedToCompanyPortalUser : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<int>(
                name: "CoreUserId",
                table: "AspNetUsers",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "nvarchar(max)",
                oldNullable: true);

            migrationBuilder.AddColumn<string>(
                name: "FirstName",
                table: "AspNetUsers",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "LastName",
                table: "AspNetUsers",
                nullable: true);
        }

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

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

            migrationBuilder.AlterColumn<string>(
                name: "CoreUserId",
                table: "AspNetUsers",
                type: "nvarchar(max)",
                nullable: true,
                oldClrType: typeof(int));
        }
    }
}

Here are the related bits from Starup.cs ConfigureServices():

            services.AddDbContext<CompanyIdentityDbContext>(config =>
            {
                config.UseSqlServer(_configuration.GetConnectionString("CompanyIdentityDb"));
            });

            services.AddIdentity<CompanyPortalUser, IdentityRole>()
                    .AddDefaultTokenProviders()
                    .AddEntityFrameworkStores<CompanyIdentityDbContext>();

What I don't understand is why is it trying to create tables in the first place? Shouldn't it just be trying to execute my last migration?

I have CompanyPortal.Web set as the Startup Project. I also have the Default project in Package Manager Console set to: CompanyIdentity.DAL

Another bit of information. This solution contains two web apps: CompanyPortal.Web and CustomerPortal.Web It also contains two class libraries, one for each Identity layer: CompanyIdentity.DAL and CustomerIdentity.DAL Not sure if I'm supposed to specify something more in the Update-Database command because of that.


Solution

  • It turns out the entire solution was completely rewritten at some point and the old database was kept. The record in __EFMigrationsHistory table, MigrationId, did not match the name of the file in the Migrations folder of the project. Since there is one record, the initial migration record, I deleted the record and then inserted a record with the correct MigrationId and ProductVersion.