gog*_*gog 2 asp.net asp.net-mvc-5 asp.net-identity
在我的asp.net MVC 5项目中,我无法弄清楚为什么我会收到此错误:
Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
Invalid column name 'Description'.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试执行此方法时,我得到无效的列错误.可能是什么?
编辑:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
Ada*_*m W 12
修改角色时(例如,在IdentityModels.cs中向模型添加属性,如下所示):
public class ApplicationRole:IdentityRole
{
public string Description { get; set; }
public string AreaUsed { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在代码优先方法中,它将重新创建数据库,并且将向aspNetRoles添加3列而不是2(是的 - 我也很惊讶).附加列名称为"Discriminator"类型:nvarchar(128)not null.当您添加更多角色时,它将自动获得值"IdentityRole".我想,当自动迁移禁用未加入此列,因此你会得到这个错误"无效列名".我在使用identity 2.0的MVC 5.1项目中遇到了同样的问题
| 归档时间: |
|
| 查看次数: |
7275 次 |
| 最近记录: |