3

In my asp.net MVC 5 project, i cannot figure out why im getting this error:

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

Here is my code:

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

When i try to execute this method i get the invalid column error. what it could be?

EDIT:

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

}
gog
  • 11,788
  • 23
  • 67
  • 129

3 Answers3

13

When you modify Roles (for example add properties to your model in IdentityModels.cs like below):

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

In code-first approach it will re-create database and 3 columns will be added to aspNetRoles instead of 2 (yes - I was surprised too). The additional column name is "Discriminator" type: nvarchar(128) not null. When you're adding more roles it will automatically get value "IdentityRole". I guess when automatic migrations are disabled this column is not being added and as a result you will be getting this error "Invalid column name". I had the same problem in my MVC 5.1 project with identity 2.0

Adam W
  • 263
  • 1
  • 4
  • 12
  • in your migration add this , this is my code public partial class testingthisshit2 : DbMigration { public override void Up() { AddColumn("dbo.AspNetUsers", "Discriminator", c => c.String(nullable: false, maxLength: 128)); } public override void Down() { DropColumn("dbo.AspNetUsers", "Discriminator"); } } – General Electric Oct 27 '15 at 22:12
  • @Adam, in your response, you say that IdentityRole is the value set in Discriminator column, but, in fact, is ApplicationRole what is set. – antonio_mg Sep 13 '16 at 21:18
  • @Antonio_mg - I'm looking at my database now and I see "IdentityRole" as a value for all records in Discriminator column. Maybe something has changed since version 5.1 Are you on "Core" version maybe ? – Adam W Sep 15 '16 at 19:26
4

Taking the previous response you have to do the following migration to get rid of this error

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

I was having this issue and doing that fixed it.

General Electric
  • 1,176
  • 3
  • 21
  • 44
1

I add this same issue to fix add [NotMapped] as attribute ApplicationRole and ApplicationDbContext should inherit 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>
 { ......

hope this help more details can be found at
https://entityframeworkcore.com/knowledge-base/48712868/avoid--discriminator--with-aspnetusers--aspnetroles----aspnetuserroles