Question

Here is my ApplicationRole:

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }

    public string Description { get; set; }
}

Here is my ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

Here is the Migration add-migration created:

namespace MyApp.MigrationsMembership
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class RolesFields : DbMigration
    {
        public override void Up()
        {
            DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
            CreateTable(
                "dbo.AspNetRoles",
                c => new
                    {
                        Id = c.String(nullable: false, maxLength: 128),
                        ProperName = c.String(nullable: false, maxLength: 50),
                        Description = c.String(),
                        Name = c.String(nullable: false, maxLength: 256),
                    })
                .PrimaryKey(t => t.Id);

            AddForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles", "Id", cascadeDelete: true);
            DropTable("dbo.AspNetRoles");
        }

        public override void Down()
        {
            CreateTable(
                "dbo.AspNetRoles",
                c => new
                    {
                        Id = c.String(nullable: false, maxLength: 128),
                        Name = c.String(nullable: false, maxLength: 256),
                    })
                .PrimaryKey(t => t.Id);

            DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
            DropTable("dbo.AspNetRoles");
            AddForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles", "Id", cascadeDelete: true);
        }
    }
}

Why is it creating a new AspNetRoles table...and then trying to DropTable on it?

Here is the error message from Package Manager Console:

There is already an object named 'AspNetRoles' in the database.

UPDATE: I removed "beta" from the title because this same issue continues to happened since 2.0.0 was released. Every migration I have created that required dropping a table does the DropTable last.

Was it helpful?

Solution

The work around is obvious: Simply change the order of the TransactSQL commands created in the migration file before running Update-Database. Somebody should fix this though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top