Invalid Column Name Discriminator when try to create Role

asp.net, asp.net-identity, asp.net-mvc-5

Solution

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

Problem

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

Original source

Related problems