简体   繁体   English

使用MySQL数据库的ASP.NET身份给我的身份表一个错误的名称

[英]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. 我有一个托管在Azure中的mysql数据库,并且我的计算机上有MVC应用程序。 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. 表xxxxxxxx.aspnetusers不存在。

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 . 但是我不知道为什么在表中添加名称my_aspnet_users而不是aspnetusers

I followed this article as a guide: https://www.codeproject.com/Tips/788357/How-to-set-up-application-using-ASP-NET-Identity-w 我按照这篇文章作为指南: https : //www.codeproject.com/Tips/788357/How-to-set-up-up-application-using-ASP-NET-Identity-w

I also tried to edit my ApplicationDbContext: 我也尝试编辑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");
    }
}

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. 但是您可以将DbContext配置为使用实际的DbContext ,如下所示。
Do the same for each table. 对每个表执行相同的操作。

modelBuilder.Entity<IdentityUser>().ToTable("my_aspnet_users");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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