gog
gog

Reputation: 12988

Invalid Column Name Discriminator when try to create Role

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

}

Upvotes: 4

Views: 12362

Answers (3)

donprecious iyeritufu
donprecious iyeritufu

Reputation: 169

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

Upvotes: 1

General Electric
General Electric

Reputation: 1236

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.

Upvotes: 4

Adam W
Adam W

Reputation: 285

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

Upvotes: 13

Related Questions