0

I'm seeding the User identity table using EF core configurations, this is my method.

  public void Configure(EntityTypeBuilder<IdentityUser> builder)
  {
    var hasher = new PasswordHasher<IdentityUser>();
    var adminUser = new IdentityUser
    {
      Id = Id,
      UserName = AdminUserName,
      NormalizedUserName = AdminUserName,
      Email = AdminUserName,
      NormalizedEmail = AdminUserName,
      EmailConfirmed = true,
      ConcurrencyStamp = ConcurrencyStamp,
      SecurityStamp = SecurityStamp
    };

    adminUser.PasswordHash = hasher.HashPassword(adminUser, Password);
    
    builder.HasKey(u => u.Id);
    builder.Property(u => u.Id).ValueGeneratedNever();
    builder.Property(u => u.ConcurrencyStamp).ValueGeneratedNever();
    builder.Property(u => u.PasswordHash).ValueGeneratedNever();
    builder.Property(u => u.SecurityStamp).ValueGeneratedNever();

    builder.HasData(adminUser);
  }

It works great when creating the first migration, applying all the fields i listed in the code. The issue arises when i create a second migration (even without changing anything), it always create a migration trying to change the password hash, even if i specified to never generate the value.

 public partial class UserAndRoleSeed3 : Migration
 {
     /// <inheritdoc />
     protected override void Up(MigrationBuilder migrationBuilder)
     {
         migrationBuilder.UpdateData(
             schema: "Users",
             table: "AspNetUsers",
             keyColumn: "Id",
             keyValue: "0fbad743-9b2c-4eac-aa17-957880a5465d",
             column: "PasswordHash",
             value: "AQAAAAIAAYagAAAAELfdYk76pRqQjoKQM9cOrCEplDXzB2mFhQdaCByEIBU4ng6fb9FtK7rY6sCLKwhS8A==");
     }

     /// <inheritdoc />
     protected override void Down(MigrationBuilder migrationBuilder)
     {
         migrationBuilder.UpdateData(
             schema: "Users",
             table: "AspNetUsers",
             keyColumn: "Id",
             keyValue: "0fbad743-9b2c-4eac-aa17-957880a5465d",
             column: "PasswordHash",
             value: "AQAAAAIAAYagAAAAELG0nqfYzxLsa3gKh8PlUIx23oUZOe4RJjrk1DRPSdYvdkDqoQsTVhCApmGOx0awVw==");
     }
 }

How can i avoid this issue with migrations? Thanks

2
  • Check this out, this helped me with this exact problem recently: stackoverflow.com/questions/34343599/… Commented Apr 28, 2024 at 2:46
  • 1
    Already read that post but sadly no-one is getting the issue i'm describing here. Commented Apr 28, 2024 at 9:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.