Hi @Sherpa,
Here is a whole working demo you could follow:
1.Model
public class ApplicationUser : IdentityUser
{
[ProtectedPersonalData]
[Column("EmailAddress")]
public override string? Email { get; set; }
}
2.DbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(entity =>
{
entity.Property(e => e.Email)
.HasMaxLength(256)
.HasColumnName("EmailAddress");
});
}
}
3.Program.cs
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = false)
.AddEntityFrameworkStores<ApplicationDbContext>();
4.Ensure there are no additional properties named EmailAddress1
in your ApplicationUser
class or in the configuration.
5.Add a new migration to update the database schema to reflect these changes.
PM> add-migration init1
PM> update-database
6.Ensure that the generated migration file(xxxxxxx_init1.cs) correctly updates the schema. The migration should rename the column from Email
to EmailAddress
and should not create an EmailAddress1 column. It should look similar to this:
public partial class init1 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "Email",
table: "AspNetUsers",
newName: "EmailAddress");
7.Check your DbContextModelSnapshot
to ensure that it matches your configuration. It should look similar to this:
b.Property<string>("Email")
.HasMaxLength(256)
.HasColumnType("nvarchar(256)")
.HasColumnName("EmailAddress");
If still not working, try to remove all the previous migration files and delete the database, then remigrate and update the database again.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Rena