I want to add a new field on my AspNetUsers table. I want to do that with EF Migration.
In my InitialCreate migration I only added FullName (last field in the table) - which built successfully.
On my second migration I want to add another field. I updated my ApplicationUser which derives from IdentityUser
public class ApplicationUser : IdentityUser
{
[Column(TypeName = "nvarchar(150)")]
public string FullName { get; set; }
public string RefreshTokensAuth { get; set; }
}
The problem is that after I ran the command in PM:
PM> Add-Migration -Context AuthenticationContext AddUsersSession
my migration contains modifications for other tables as well as seen below (AspNetUserTokens, AspNetUserLogins), not only to 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);
}
}
Of course, when I run
update-database -context AuthenticationContext
I ran into errors.
Did someone experienced this? Any hint will be helpful. Thank you!
CodePudding user response:
UPDATE:
I found that my InitialCreate migration had some problems.
I replicated by running Add-Migration without any changes to IdentityUser model and still got Up and Down autogenerated changes.
To fix the issue I ran Add-Migration as before, altered the migration by removing the fields (lines of code) that would update the tables that were not supposed to be updated, and then run
Update-Database -Context AuthenticationContext
The next migrations (if you plan to do others) will run just fine as I did a further one to test and check.
Thanks!