【问题标题】:Change Identity Role Claims Pk type as Guid将身份角色声明 Pk 类型更改为 Guid
【发布时间】:2021-08-05 06:17:55
【问题描述】:

我创建了一个数据上下文,其中所有身份表都以 Guid 作为主键,但迁移后的 IdentityUserClaimIdentityRoleClaim 表仍然是仍然有 int 作为主键。有什么方法可以将其更改为 Guid 作为主键

public class DataContext: IdentityDbContext<IdentityUser<Guid>, IdentityRole<Guid>, Guid, IdentityUserClaim<Guid>, IdentityUserRole<Guid>, IdentityUserLogin<Guid>, IdentityRoleClaim<Guid>, IdentityUserToken<Guid>>
{
    public DataContext(DbContextOptions<DataContext> options): base(options)
    {
            
    }
}

enter image description here

【问题讨论】:

  • 我正在使用 .net core 5.0 和 entityframework 核心代码优先方法
  • 你能显示迁移吗?

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


【解决方案1】:

Customize Identity Model 说:

IdentityUserClaim 的 TKey 是为 PK 指定的类型 的用户。在这种情况下,TKey 是字符串,因为默认值是 用过的。这不是 UserClaim 实体类型的 PK 类型。

IdentityUserClaim 类如下所示:

public class IdentityUserClaim<TKey> where TKey : IEquatable<TKey>
{
    public virtual int Id { get; set; }

    public virtual TKey UserId { get; set; }

    public virtual string ClaimValue { get; set; }

    public virtual void InitializeFromClaim(Claim claim);

    public virtual Claim ToClaim();
}

【讨论】:

  • 感谢您的帮助。有没有其他方法可以用 Guid 覆盖主键
  • 尝试创建自己的类 MyIdentityUserClaim 并在 IdentityDbContext 和其他任何地方使用它,而不是 IdentityUserClaim,我不知道它是否会起作用。
  • 感谢您的帮助和支持。我确实试过了,但也没有用。
【解决方案2】:

可以使用 EF Core 对身份表的非主键字段(例如 IdentityUser 的自定义身份用户表)应用更新迁移。但是,对于 ASP.NET Core Identity User、Roles、Claims 表上的新主键类型,您不能应用 EF Core 更新脚手架。

建议完全删除并重新创建。

应用以下内容:

  1. 删除标识表和初始 EF Core 迁移。这是 Microsoft 的建议:

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-5.0

  1. 修改数据库上下文类(已经完成)。

  2. 在启动配置服务中添加以下内容以使用通用用户:

    services.AddIdentity() .AddEntityFrameworkStores();

  3. 根据上下文重新创建数据库表。这可以使用:

    context.Database.EnsureCreated();

在启动配置()中。

【讨论】:

  • 我已经做了很多次这些事情,但是 userclaims 表的 pk 仍然是 int
猜你喜欢
  • 2018-02-10
  • 2014-04-28
  • 2021-07-20
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 2023-02-21
  • 1970-01-01
相关资源
最近更新 更多