2014-02-24 77 views
1

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

} 
+0

ApplicationDbContext中有什么? – BlackICE

+0

@BlackICE查看我的编辑新章节。谢谢 – gog

+0

你有多少自定义UserManager过程?你是否在用户/角色/成员类之一上设置了继承层次结构?如果是这样,请看这里:http://stackoverflow.com/a/6586990/264607 – BlackICE

回答

10

当你修改角色(例如属性添加到您的模型IdentityModels.cs像下面):

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

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

+0

那么,我该如何解决这个问题? –

+0

在迁移添加这个,这是我的代码公共部分类testingthisshit2:DbMigration { 公共覆盖无效向上(){ AddColumn( “dbo.AspNetUsers”, “鉴别”,C => c.String(可为空: false,maxLength:128)); } public override void Down() DropColumn(“dbo.AspNetUsers”,“Discriminator”); } } –

+0

@Adam,在您的回复中,您说IdentityRole是Discriminator列中设置的值,但实际上是ApplicationRole设置的值。 –

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

我是有这个问题,这样做固定的它。