【问题标题】:ASP.NET Identity using a MySQL Database gives my identity tables a wrong name使用 MySQL 数据库的 ASP.NET 标识为我的标识表提供了错误的名称
【发布时间】:2018-07-25 20:59:11
【问题描述】:

我有一个托管在 Azure 中的 mysql 数据库,并且我的计算机上有 MVC 应用程序。 我想使用 Identity 框架来跟踪我的用户。身份表是在我的数据库中生成的,但它们的名称都错误:

因为当我想在我的注册页面上添加新用户时,我收到以下错误:

表 xxxxxxxx.aspnetusers 不存在。

我知道这是因为我的表名错误。 但我不知道为什么添加的表格名称为 my_aspnet_users 而不是 aspnetusers

我按照这篇文章作为指导: https://www.codeproject.com/Tips/788357/How-to-set-up-application-using-ASP-NET-Identity-w

我还尝试编辑我的 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&lt;IdentityUser&gt;().ToTable("my_aspnet_users");?其他表也类似。
  • 这不起作用,因为它不会像现在一样重命名表。所以如果我改变它,它会保持不变。但我会尝试确定。
  • 这不是为了重命名表格,而是让EntityFramework DbContext知道要使用哪些表格,有替代名称。
  • 确实有效,如果您愿意,可以发布答案,我会批准

标签: c# mysql asp.net asp.net-mvc identity


【解决方案1】:

我不会试图弄清楚为什么你的表名是原样的,
但您可以配置您的DbContext 以使用实际的, 如下所示。
对每个表执行相同操作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 2016-05-05
    • 2010-09-30
    • 1970-01-01
    相关资源
    最近更新 更多