【问题标题】:Unable to change Asp Identity Table Names..... Asp .Net Core 2?无法更改 Asp 身份表名称.....Asp .Net Core 2?
【发布时间】:2018-02-17 12:35:51
【问题描述】:

我想更改身份表名称。我搜索了一下,发现了以下方法:

第一个:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);


    builder.Entity<ApplicationUser>(entity =>
    {
        entity.ToTable(name: "Users");
    });

    builder.Entity<ApplicationRole>(entity =>
    {
        entity.ToTable(name: "Roles");
    });

    builder.Entity<ApplicationRoleClaim>(entity =>
    {
        entity.ToTable(name: "RoleClaims");
    });

    builder.Entity<ApplicationUserRole>(entity =>
    {
        entity.ToTable(name: "UserRoles");
    });

    builder.Entity<ApplicationUserLogin>(entity =>
    {
        entity.ToTable(name: "UserLogins");
    });

    builder.Entity<ApplicationUserClaim>(entity =>
    {
        entity.ToTable(name: "UserClaims");
     });

    builder.Entity<ApplicationUserToken>(entity =>
    {
        entity.ToTable(name: "UserTokens");
    });
 }

第二个:

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<ApplicationUser>().ToTable("Users");
        builder.Entity<ApplicationRole>().ToTable("Roles");
        builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
        builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
        builder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
        builder.Entity<ApplicationUserToken>().ToTable("UserTokens");
        builder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
    }

所有通用名称,如 ApplicationUser、ApplicationRole……都以 int 作为它们的主键。

ApplicationDbContextStartUp 如下所示

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>

启动类

       services.AddDbContext<ApplicationDbContext>(options =>
            options.UseMySql(Configuration.GetConnectionString("MySQLConnection")));

        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

如果您注意到,在启动类中,我没有在 AddEntityFrameworkStores&lt;ApplicationDbContext&gt;(). 上添加 int 因为,编译器错误一直显示为 “不支持...。 "。

我做了以下删除旧数据库和迁移

$ drop-database
$ remove-migration

添加新配置

$ add-migration Initial
$ update-database

我发现的结果是,只有用户和角色表被改变,而其他的没有改变(AspUserRoles,AspUserClaim.....)。

仅供参考:

我正在使用 Visual Studio 2017。我的项目使用默认的 Web 应用程序,并选择了 个人用户帐户.NET Core 2。 我还使用 Pomela.EntityFramework.MySql 作为数据库提供者。

我的问题是:我做错了什么?或者发生了什么变化?

【问题讨论】:

    标签: c# asp.net entity-framework asp.net-core asp.net-identity


    【解决方案1】:

    您需要告诉IdentityDbContext 您所有的自定义类型。为此,您需要扩展传递给IdentityDbContext 的泛型,如下所示:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser,
        ApplicationRole, int, ApplicationUserClaim, ApplicationUserRole,
        ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>
    

    【讨论】:

    • 我有一个相同的代码来定义我的 DbContext 类,并使用了与 @ash 使用的相同的代码,但所有身份表在数据库中都有 AspNet 前缀!请问你有什么秘诀4吗?
    • @RAM 没有什么特别突出的。如果您决定将此作为一个新问题提出您的具体细节,请随时告诉我,我会看一看。
    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2021-03-21
    • 1970-01-01
    相关资源
    最近更新 更多