error when reading asp.net membership tables in MySQL

℡╲_俬逩灬. 提交于 2019-12-12 05:48:40

问题


I have an asp.net website that uses mysql for membership. the web app works well when i am connecting to mysql hosted on windows OS but throws an error due to table name (case-sensitivity) on Linux.

The error is not relavant to my tables, but it is related to internal MySQL adapter classes when calling to authenticate a user.

Here is the error:

Table 'dbname.AspNetUsers' doesn't exist

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: MySql.Data.MySqlClient.MySqlException: Table 'dbname.AspNetUsers' doesn't exist

Given, i have aspnetusers table with lower case in my database. the error raises only when i change the host from mySQL on windows to Linux.

Here is the line of code that throws the exception:

public static ApplicationSignInManager Create(IdentityFactoryOptions<ApplicationSignInManager> options, IOwinContext context)
        {
            return new ApplicationSignInManager(context.GetUserManager<ApplicationUserManager>(), context.Authentication);
        }

Also note, i don't have any membership tables in the asp.net web app since i am using the configs for (membership, role provider) to connect and authenticate users.

Any help is appreciated it.


回答1:


I found that you can re-map table names for identity and membership provider in asp.net by overriding the OnModelCreation method in IdentityModels.cs.

Here is the solution:

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

                modelBuilder.Entity<ApplicationUser>().ToTable("aspnetusers");
                modelBuilder.Entity<IdentityRole>().ToTable("aspnetroles");
                modelBuilder.Entity<IdentityUserRole>().ToTable("aspnetuserroles");
                modelBuilder.Entity<IdentityUserClaim>().ToTable("aspnetuserclaims");
                modelBuilder.Entity<IdentityUserLogin>().ToTable("aspnetuserlogins");

}

You notice that explicitly set all standard asp.net membership table names to lower case as i have it in my database which was causing the issue.

Now i can connect, authenticate and manage membership from my existing web app that is connected to Linux based MySQL DB.

Hope this helps.



来源:https://stackoverflow.com/questions/40509997/error-when-reading-asp-net-membership-tables-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!