2014-02-24 56 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"); 
     } 
    } 
} 

我是有這個問題,這樣做固定的它。