尝试创建角色时无效的列名称识别符

时间:2014-02-24 15:51:18

标签: asp.net asp.net-mvc-5 asp.net-identity

在我的asp.net MVC 5项目中,我无法弄清楚为什么我会收到此错误:

Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
Invalid column name 'Description'.

这是我的代码:

 RoleManager<IdentityRole> _roleManager = new RoleManager<IdentityRole>(
        new RoleStore<IdentityRole>(new ApplicationDbContext()));

    UserManager<ApplicationUser> _userManager = new UserManager<ApplicationUser>(
        new UserStore<ApplicationUser>(new ApplicationDbContext()));


    public bool CreateRole(string name, string description = "")
    {
        var idResult = _roleManager.Create(new IdentityRole(name)).Succeeded;
        return idResult;
    }

当我尝试执行此方法时,我收到无效的列错误。它可能是什么?

编辑:

ApplicationDbContext

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        if (modelBuilder == null)
        {
            throw new ArgumentNullException("modelBuilder");
        }
        modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");

        EntityTypeConfiguration<ApplicationUser> table =
            modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");

        table.Property((ApplicationUser u) => u.UserName).IsRequired();

       modelBuilder.Entity<ApplicationUser>().HasMany<IdentityUserRole>((ApplicationUser u) => u.Roles);
        modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
            new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");



        entityTypeConfiguration.HasRequired<IdentityUser>((IdentityUserLogin u) => u.User);
        EntityTypeConfiguration<IdentityUserClaim> table1 =
            modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");

        table1.HasRequired<IdentityUser>((IdentityUserClaim u) => u.User);

        // Add this, so that IdentityRole can share a table with ApplicationRole:
        modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");

        // Change these from IdentityRole to ApplicationRole:
        EntityTypeConfiguration<ApplicationRole> entityTypeConfiguration1 =
            modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");

        entityTypeConfiguration1.Property((ApplicationRole r) => r.Name).IsRequired();
    }

}

3 个答案:

答案 0 :(得分:11)

修改角色时(例如,在下面的IdentityModels.cs中为模型添加属性):

public class ApplicationRole:IdentityRole
{
    public string Description { get; set; }
    public string AreaUsed { get; set; }
}

在代码优先方法中,它将重新创建数据库,并且将向aspNetRoles添加3列而不是2(是的 - 我也很惊讶)。附加列名称为“Discriminator”类型:nvarchar(128)not null。 当您添加更多角色时,它将自动获得值“IdentityRole”。 我想当禁用自动迁移时,不会添加此列,因此您将收到此错误“无效的列名称”。我在带有identity 2.0的MVC 5.1项目中遇到了同样的问题

答案 1 :(得分:2)

采取之前的回复,您必须执行以下迁移以消除此错误

namespace Domain.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class YourMigration : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.AspNetUsers", "Discriminator", c => c.String(nullable: false, maxLength: 128));           
        }

        public override void Down()
        {
            DropColumn("dbo.AspNetUsers", "Discriminator");
        }
    }
}

我遇到了这个问题并做了修复。

答案 2 :(得分:0)

我添加了相同的问题来修复将添加[NotMapped]作为属性ApplicationRole的问题 并且ApplicationDbContext应该继承IdentityDbContext<TUser, IdentityRole, string>

[NotMapped]
public class ApplicationRole:IdentityRole
{
    public string Description { get; set; }
    public string AreaUsed { get; set; }
} 

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
 { ......

希望此帮助 更多详细信息,请参见
https://entityframeworkcore.com/knowledge-base/48712868/avoid--discriminator--with-aspnetusers--aspnetroles----aspnetuserroles