首页 文章

EF代码优先迁移在扩展标识角色类时创建额外的列

提问于
浏览
1

我打开VS2013并创建了一个带有标识1的空MVC项目,我想扩展Identity Role Class,为AspNetRoles添加一个额外的字段,所以我将这个类添加到Models文件夹:

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中创建并在类下面创建:

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         
   {

但是你可以看到迁移创建了一个额外的列(Discriminator),我不知道那是什么 .

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

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中,它抛出异常:“执行命令定义时发生错误 . 有关详细信息,请参阅内部异常.---> System.Data.SqlClient.SqlException:无效的列名'Discriminator'”

编辑:这是我的背景

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

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

1 回答

  • 1

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

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

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

相关问题