【发布时间】:2022-01-13 05:49:19
【问题描述】:
我想在我的 AspNetUsers 表中添加一个新字段。我想通过 EF 迁移来做到这一点。
在我的 InitialCreate 迁移中,我只添加了 FullName(表中的最后一个字段)——它已成功构建。
在我的第二次迁移中,我想添加另一个字段。我更新了派生自 IdentityUser 的 ApplicationUser
public class ApplicationUser : IdentityUser
{
[Column(TypeName = "nvarchar(150)")]
public string FullName { get; set; }
public string RefreshTokensAuth { get; set; }
}
问题是我在 PM 中运行命令后:
PM> Add-Migration -Context AuthenticationContext AddUsersSession
我的迁移包含对其他表的修改,如下所示(AspNetUserTokens、AspNetUserLogins),不仅仅是对 AspNetUsers:
public partial class AddUsersSession : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Name",
table: "AspNetUserTokens",
maxLength: 128,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(450)");
migrationBuilder.AlterColumn<string>(
name: "LoginProvider",
table: "AspNetUserTokens",
maxLength: 128,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(450)");
migrationBuilder.AlterColumn<string>(
name: "FullName",
table: "AspNetUsers",
type: "nvarchar(150)",
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(150",
oldNullable: true);
migrationBuilder.AddColumn<string>(
name: "RefreshTokensAuth",
table: "AspNetUsers",
nullable: true);
migrationBuilder.AlterColumn<string>(
name: "ProviderKey",
table: "AspNetUserLogins",
maxLength: 128,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(450)");
migrationBuilder.AlterColumn<string>(
name: "LoginProvider",
table: "AspNetUserLogins",
maxLength: 128,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(450)");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "RefreshTokensAuth",
table: "AspNetUsers");
migrationBuilder.AlterColumn<string>(
name: "Name",
table: "AspNetUserTokens",
type: "nvarchar(450)",
nullable: false,
oldClrType: typeof(string),
oldMaxLength: 128);
migrationBuilder.AlterColumn<string>(
name: "LoginProvider",
table: "AspNetUserTokens",
type: "nvarchar(450)",
nullable: false,
oldClrType: typeof(string),
oldMaxLength: 128);
migrationBuilder.AlterColumn<string>(
name: "FullName",
table: "AspNetUsers",
type: "nvarchar(150",
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(150)",
oldNullable: true);
migrationBuilder.AlterColumn<string>(
name: "ProviderKey",
table: "AspNetUserLogins",
type: "nvarchar(450)",
nullable: false,
oldClrType: typeof(string),
oldMaxLength: 128);
migrationBuilder.AlterColumn<string>(
name: "LoginProvider",
table: "AspNetUserLogins",
type: "nvarchar(450)",
nullable: false,
oldClrType: typeof(string),
oldMaxLength: 128);
}
}
当然,当我跑步时
update-database -context AuthenticationContext
我遇到了错误。
有人经历过吗?任何提示都会有所帮助。谢谢!
【问题讨论】:
-
你可以试试 PM> Add-Migration "UniqueMigrationName" 而不指定上下文吗?
-
@PramiGawande - 我会在一分钟内完成,但我有另一个我使用的数据库的上下文...我不确定它是否会有所帮助。
-
好的。我假设你只有一个上下文。
标签: asp.net entity-framework-core migration database-migration ef-core-3.1