尝试创建角色时无效的列名区分符

问题描述 投票:2回答:3

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

}
asp.net asp.net-mvc-5 asp.net-identity
3个回答
12
投票

[当您修改角色时(例如,如下所示在IdentityModels.cs中向模型添加属性):

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

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


4
投票

采取上一个响应,您必须进行以下迁移才能消除此错误

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

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


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

© www.soinside.com 2019 - 2024. All rights reserved.