Search code examples
asp.netasp.net-web-apioauthasp.net-identity

Can't login using web api identity


I'm trying to implement a web api for authentication in a local project and to learn that process I'm using asp.net's tutorial Here The errors I'm getting with this mirror the issues I'm having with my own so it felt like a good starting point to troubleshoot. The only changes I've made are pointing it to my local SQL db.

I don't like the naming convention of the tables it creates, so I added an override to specify the table names that I got from an online resource. When I add this override it's throwing a status code 500 internal server error:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("User").Property(p => p.Id).HasColumnName("UserID");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles").Property(p => p.Id).HasColumnName("RoleID");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim").Property(p => p.Id).HasColumnName("UserClaimID");
    }
}

How can I keep the functionality and change the table names?


Solution

  • So the issue was that I ran it once, it created the default table schema, then I ran it again and it was expecting a code migration to update the tables. I expected it to generate new tables, so that was the confusion. I deleted the entire database and kept the same override method in place and it worked.