简体   繁体   中英

How to allow users to have more than one email using Asp.net Core Identity?

I'm trying to allow my users to have more than one email. I have tried adding collection to the user but then I can access both email (the default one inherited from IndentityUser) and emails (my collection containg the user emails).

User.cs

public ICollection<UserEmail> Emails { get; set; }

UserEmail.cs

public class UserEmail : BaseEntity
{
    public string UserId { get; set; }
    public User User { get; set; }

    public string Email { get; set; }
    public string NormalizedEmail { get; set; }

    public DateTime DateAdded { get; set; }
    public DateTime DateVerified { get; set; }

    public bool Verified { get; set; }
    public bool Primary { get; set; }
}

You can remove the database field from AspNetUsers table and can ignore the "unused email and normalised email from inheriting the identity user" . Please override OnModelCreating method as stated here.

 protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      base.OnModelCreating(modelBuilder);
      modelBuilder.Entity<ApplicationUser>().Ignore(e => e.Email).Ignore(e => e.NormalizedEmail);
  }

Here the ApplicationUser class is the entity class that is extended from IdentityUser or you can use IdentityUser directly if there is no derived class .

Once you changed your code Add a new migration and update database using migration command. Once your database has been updated check AspNetUsers table and will notice that Email and NormalizedEmail columns have been deleted from the table.

After that you will get null value for those two fields. Please check if it is helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM