Search code examples
c#asp.netentity-frameworkasp.net-identity

Ignore UserName property with ASP.NET Identity


My app uses ASP.NET identity but makes no use of the UserName property as users log in using Email instead. So I'd like to remove it from the database table to keep things tidy, which I've done with the following code:

public class UserEntityConfiguration : EntityTypeConfiguration<User>
{
    public UserEntityConfiguration()
    {
        ToTable("User");

        Property(u => u.Id).HasColumnOrder(0);
        Property(u => u.Title).HasMaxLength(50).HasColumnOrder(1);
        Property(u => u.FullName).HasMaxLength(256).IsRequired().HasColumnOrder(2);
        Property(u => u.Email).HasMaxLength(512).IsRequired().HasColumnOrder(3);
        Property(u => u.EmailConfirmed).HasColumnOrder(4);
        Property(u => u.PasswordHash).HasColumnOrder(5);
        Property(u => u.SecurityStamp).HasMaxLength(256).HasColumnOrder(6);
        Ignore(u => u.UserName);  // not used
        Ignore(u => u.PhoneNumber);  // not used
        Ignore(u => u.PhoneNumberConfirmed);  // not used

    }
}

If I leave the UserName property unset, I get the run-time error

Name cannot be null or empty.

If I then try to set the property, I get this error instead:

The specified type member 'UserName' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

because there isn't a database column to map the property to.

Is it possible to use ASP.NET Identity without having a UserName column in the User table (that in my case would not be used)?


Solution

  • I don't think that is possible with the default IdentityUser. The UserName Property is also used for authentification.

    You maybe have to write your own identity or just 'ignore' it (let it be).