EF Code First 迁移在扩展身份角色时创建额外的列 Class

EF Code First Migrations creating extra Column when Extending the Identity Role Class

我打开 VS2013 并创建了一个身份为 1 的空 MVC 项目,我想扩展身份角色 Class,向 AspNetRoles 添加一个额外的字段,所以我将这个 class 添加到模型文件夹:

  public class ApplicationRole : IdentityRole
    {
        public ApplicationRole() : base() { }
        public ApplicationRole(string name, string title)
            : base(name)
        {
            this.Title = title;
        }
        public virtual string Title { get; set; }
    }

然后在 PowerShell > enable-migrations>add-migration mig1 中创建 class:

public partial class mig1 : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.AspNetRoles",
                c => new
                    {
                        Id = c.String(nullable: false, maxLength: 128),
                        Name = c.String(nullable: false, maxLength: 256),
                        Title = c.String(),
                        Discriminator = c.String(nullable: false, maxLength: 128),  // The rogue column
                    })
                .PrimaryKey(t => t.Id)
                .Index(t => t.Name, unique: true, name: "RoleNameIndex");    
        {
         //Another tables         
   {

但是正如您所见,迁移创建了一个额外的列(鉴别器),我不知道那是什么。

这不是我想要的,所以我评论了它并在配置中播种 class 我添加了以下代码:

protected override void Seed(another.Models.ApplicationDbContext context)
        {

            if (!context.Roles.Any(r => r.Name == "AppAdmin"))
            {
                var store = new RoleStore<ApplicationRole>(context);
                var manager = new RoleManager<ApplicationRole>(store);
                var role = new ApplicationRole { Name = "AppAdmin", Title = "Admin" };

                // Create: Create a role.    
                manager.Create(role);
            }
            if (!context.Roles.Any(r => r.Name == "Customer"))
            {
                var store = new RoleStore<ApplicationRole>(context);
                var manager = new RoleManager<ApplicationRole>(store);
                var role = new ApplicationRole { Name = "Customer",Title="usualuser" };

            // Create: Create a role.    
            manager.Create(role);
        }
    } 

然后在 PowerShell > update-database 中,它抛出了一个异常: "An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'"

已编辑:这是我的背景

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("ShopDB", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

您的上下文未指定您正在使用您的自定义角色 class,因此 Entity Framework 扫描项目以查找从基础 IdentityRole 继承的任何 classes class 并假设您可能想要使用 TPH(每个层次结构 table),这样可以将 IdentityRoleApplicationRole 对象存储在同一个 DbSet 中。为此,它添加了一个 Discriminator 列来区分类型。

要解决此问题,您的上下文应继承自允许您指定类型的 other IdentityDbContext。例如:

public class ApplicationDbContext : 
    IdentityDbContext<ApplicationUser, ApplicationRole, string, 
        IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    //Snip
}