【发布时间】:2017-06-16 22:47:39
【问题描述】:
我有一个使用 ASP.NET Identity 2.0 和 DB 的项目,由 ASP.NET Identity 1.0 创建。我想更新我的数据库。我执行以下操作:
- 启用迁移:
Enable-Migrations -ProjectName Web
检查上下文是否针对现有数据库...
为项目 Web 启用 Code First 迁移。
- 添加迁移(我的项目有 2 个连接字符串,我选择合适的,带有“旧”数据库表):
Add-Migration -Name UpdateIdentity -ProjectName Web -ConnectionStringName MainContext
脚手架迁移“UpdateIdentity”。
此迁移文件的设计器代码包含您的快照 当前的 Code First 模型。此快照用于计算 下一次迁移时对模型的更改。如果你 对您想要包含在此中的模型进行其他更改 迁移,然后您可以通过运行“添加迁移”来重新构建它 再次更新身份。
但它会生成以下脚本:
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
CompanyId = c.Int(nullable: false),
SMSnumber = c.String(),
FullName = c.String(),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
CreateTable(
"dbo.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.String(nullable: false, maxLength: 128),
ClaimType = c.String(),
ClaimValue = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.AspNetUserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
}
它试图从零开始创建表,但我需要一个脚本来修改。为什么会这样以及如何创建正确的脚本?
【问题讨论】:
标签: asp.net-mvc asp.net-identity asp.net-identity-2 entity-framework-migrations