How to remove an unwanted autogenerated field in code-first migration of Asp. Net Core 6

Sherpa 326 Reputation points
2024-05-25T21:59:47.9533333+00:00

I am working on Asp.Net Core 6.0 MVC. I have customized IdentityUser by inheriting and creating a new class, ApplicationUser. In this class, I have added my custom properties. My ApplicationUser doesn't have a field named EmailAddress1. However, I have renamed the Email field into EmailAddress. When running the migration, it automatically creates a field named EmailAddress1, in addition to EmailAddress. This is how my DbContextModelSnapshot.cs looks like.

b.Property<string>("Email")

.HasMaxLength(256)

.HasColumnType("nvarchar(256)")

.HasColumnName("EmailAddress");

b.Property<string>("EmailAddress")

.IsRequired()

.HasMaxLength(50)

.HasColumnType("nvarchar(50)")

.HasColumnName("EmailAddress1");

Even after commenting out the second part and running a migration, it puts it back and the AspNetUsers table is updated with the EmailAddress1 column. Please help me how to get rid of this field. When I try to confirm the email when a user registers, RegisterConfirmation.cshtml page is looking for the Email in the EmailAddress1 column and since that field is not populated it errors out.

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-05-28T06:09:55.5233333+00:00

    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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.