ASP.NET Identity using a MySQL Database gives my identity tables a wrong name

Multi tool use


ASP.NET Identity using a MySQL Database gives my identity tables a wrong name
I have a mysql database hosted in Azure, and I have MVC application om my computer.
I wanted to use the Identity framework to keep track of my users. The identity tables got generated in my database, but they all have wrong names:
because when i want to add a new user on my register page i get the following error:
Table xxxxxxxx.aspnetusers doesnt exist.
I know this is because my table names are wrong.
But i have no clue why the tables got added with the name my_aspnet_users in stead of aspnetusers.
I followed this article as a guide:
https://www.codeproject.com/Tips/788357/How-to-set-up-application-using-ASP-NET-Identity-w
I also tried to edit my ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("WebShopDatabaseContext")
{
Database.SetInitializer(new MySqlInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("aspnetusers");
modelBuilder.Entity<ApplicationUser>().ToTable("aspnetusers");
modelBuilder.Entity<IdentityUserRole>().ToTable("aspnetuserroles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("aspnetuserlogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("aspnetuserclaims");
modelBuilder.Entity<IdentityRole>().ToTable("aspnetroles");
}
}
modelBuilder.Entity<IdentityUser>().ToTable("my_aspnet_users");
This wouldn't work because it doesn't rename the tables as it is now. So if i would change that it would stay te same. But il try it to be sure.
– JoaVer
yesterday
It is not for the purpose of renaming the tables, it is to let the
EntityFramework DbContext
know which tables to use, having alternative names.– pfx
yesterday
EntityFramework DbContext
that acctually worked, if you want you can post an answer and i will aprove it
– JoaVer
yesterday
1 Answer
1
I'm not going to try to figure out why your table names are as-is,
but you can configure your DbContext
to use the actual ones,
like here below.
Do the same for each table.
DbContext
modelBuilder.Entity<IdentityUser>().ToTable("my_aspnet_users");
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Did you try configure with the actual table names, like
modelBuilder.Entity<IdentityUser>().ToTable("my_aspnet_users");
? And similar for other tables.– pfx
yesterday