我有一个由 ASP.NET Identity 1.0 创建的带有 ASP.NET Identity 2.0 和 DB 的项目。我想更新我的数据库。我执行以下操作:
Enable-Migrations -ProjectName Web
检查上下文是否针对现有数据库...
为项目 Web 启用代码优先迁移。
Add-Migration -Name UpdateIdentity -ProjectName Web -ConnectionStringName MainContext
脚手架迁移“UpdateIdentity”。
此迁移文件的设计器代码包括当前 Code First 模型的快照。此快照用于在您构建下一次迁移时计算对模型的更改。如果您对要包含在此迁移中的模型进行了其他更改,则可以通过再次运行“Add-Migration UpdateIdentity”来重新构建它。
但它会生成以下脚本:
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);
}
它试图从零创建表,但我需要一个脚本来修改。为什么如此以及如何创建正确的脚本?
EF 与之前的迁移进行了比较。如果没有,它将当前模型与空数据库进行比较。所以你需要的是创建一个初始的、无代码的迁移:
add-migration MyStartPoint -IgnoreChanges
现在下一次迁移将仅是更改。因此,在您的情况下,如果您可以回滚到身份 1,进行此迁移,更新到身份 2,添加第二次迁移,那么您将获得所需的内容。见这里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句