我打开VS2013并创建了一个标识为1的空MVC项目,我想扩展标识角色类,向AspNetRoles添加一个额外的字段,因此我将该类添加到了Models Folder:
public class ApplicationRole : IdentityRole
{
public ApplicationRole() : base() { }
public ApplicationRole(string name, string title)
: base(name)
{
this.Title = title;
}
public virtual string Title { get; set; }
}
然后在PowerShell中> enable-migrations> add-migration mig1并在类下创建它:
public partial class mig1 : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
Title = c.String(),
Discriminator = c.String(nullable: false, maxLength: 128), // The rogue column
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
{
//Another tables
{
但是您可以看到迁移创建了一个额外的列(区分符),我不知道那是什么。
这不是我想要的,所以我对其进行了注释,并在配置类中添加了以下代码:
protected override void Seed(another.Models.ApplicationDbContext context)
{
if (!context.Roles.Any(r => r.Name == "AppAdmin"))
{
var store = new RoleStore<ApplicationRole>(context);
var manager = new RoleManager<ApplicationRole>(store);
var role = new ApplicationRole { Name = "AppAdmin", Title = "Admin" };
// Create: Create a role.
manager.Create(role);
}
if (!context.Roles.Any(r => r.Name == "Customer"))
{
var store = new RoleStore<ApplicationRole>(context);
var manager = new RoleManager<ApplicationRole>(store);
var role = new ApplicationRole { Name = "Customer",Title="usualuser" };
// Create: Create a role.
manager.Create(role);
}
}
然后在PowerShell> update-database中,它引发了一个异常: “执行命令定义时发生错误。有关详细信息,请参见内部异常。---> System.Data.SqlClient.SqlException:无效的列名'Discriminator'”
编辑:这是我的情况
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("ShopDB", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
答案 0 :(得分:1)
您的上下文未指定您正在使用自定义角色类,因此Entity Framework会在项目中扫描从基IdentityRole
类继承的任何类,并假定您可能要使用TPH(每个层次结构的表) ),可以将IdentityRole
和ApplicationRole
对象存储在同一DbSet
中。为此,它添加一个Discriminator
列以区分类型。
要解决此问题,您的上下文应继承自other IdentityDbContext
,允许您指定类型。例如:
public class ApplicationDbContext :
IdentityDbContext<ApplicationUser, ApplicationRole, string,
IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
//Snip
}