【问题标题】:EF Code First Migrations creating extra Column when Extending the Identity Role ClassEF Code First 迁移在扩展身份角色类时创建额外的列
【发布时间】:2018-07-17 01:14:02
【问题描述】:

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

  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         
   {

但是您可以看到迁移创建了一个额外的列(鉴别器),我不知道那是什么。

这不是我想要的,所以我对其进行了评论,并且为了在配置类中播种,我添加了以下代码:

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: Invalid column name 'Discriminator'"

已编辑:这是我的上下文

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

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

【问题讨论】:

  • Discriminator 列通常是在每个层次结构设置一个表时创建的。你的上下文是什么样的?
  • 我添加了上下文

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2015-04-01
    相关资源
    最近更新 更多