在Asp.Net Identity 2.0.0中的AspNetRoles表中创建自定义字段时出现迁移错误

时间:2014-03-01 00:12:08

标签: entity-framework asp.net-mvc-5 ef-migrations asp.net-identity

这是我的ApplicationRole:

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

    public string Description { get; set; }
}

这是我的ApplicationDbContext:

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

以下是创建的迁移添加迁移:

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);
        }
    }
}

为什么要创建一个新的AspNetRoles表...然后尝试DropTable呢?

以下是Package Manager控制台的错误消息:

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

更新:我从标题中删除了“beta”,因为自2.0.0发布以来,同样的问题仍然存在。我创建的每个需要删除表的迁移都会DropTable持续。

1 个答案:

答案 0 :(得分:2)

解决方法很明显:在运行Update-Database之前,只需更改在迁移文件中创建的TransactSQL命令的顺序。有人应该解决这个问题。