【发布时间】:2018-07-17 01:14:02
【问题描述】:
我打开VS2013并创建了一个标识为1的空MVC项目,我想扩展Identity Role Class,向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: Invalid column name 'Discriminator'"
已编辑:这是我的上下文
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("ShopDB", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
【问题讨论】:
-
Discriminator列通常是在每个层次结构设置一个表时创建的。你的上下文是什么样的? -
我添加了上下文
标签: asp.net asp.net-mvc-5 asp.net-identity entity-framework-migrations