【问题标题】:Updating ASPNetUsers primary key data type from nvarchar to bigint using code first migration in .Net Core (.Net 5)使用 .Net Core (.Net 5) 中的代码优先迁移将 ASPNetUsers 主键数据类型从 nvarchar 更新为 bigint
【发布时间】:2021-09-16 12:39:04
【问题描述】:

这是我将其他字段添加到 ASPNetUsers 表的 ApplicationDbContext.cs 代码。我注意到数据库中的 Identity 生成表将主键 Id 设置为 nvarchar (450)。如何通过代码优先迁移方法将 ASPNetUsers 的 Id 更改为 bigint (Int64)?

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(100)]
    public string FirstName { get; set; }
    [Required]
    [StringLength(100)]
    public string LastName { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(20)]
    public string SAPNo { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(150)]
    public string Position { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(150)]
    public string EmailAddress { get; set; }
    [Required]
    public Int64 DepartmentId { get; set; }
    [Required]
    public int LocationId { get; set; }
    [Required]
    public int RoleId { get; set; }
    [Column(TypeName = "VARCHAR")]
    [StringLength(20)]
    public string CreatedBy { get; set; }
    public string CreatedDate { get; set; }
    public bool IsActive { get; set; } = true;
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

【问题讨论】:

  • 为什么要把users表的pk改成int?使用Guids 作为 users/roles 表上的主键的有趣之处在于它们是随机的,不能那么容易猜到。

标签: asp.net-core asp.net-core-mvc asp.net-identity asp.net-core-5.0


【解决方案1】:

我在YouTube 上看到了一个与 EF Core 迁移相关的视频,并且能够将其应用到我的代码中。

1 - 我将 ApplicationDbContext.cs 中的 ApplicationUser 类从 ApplicationUser : IdentityUser 更改为 ApplicationUser : IdentityUser&lt;Int64&gt;,以便 PK 将更新为数据库中的 bigint。如果您希望 PK 仅在数据库中为 int,则可以将其设置为 int。

public class ApplicationUser : IdentityUser<Int64>
{
    [Required]
    [StringLength(100)]
    public string FirstName { get; set; }
    [Required]
    [StringLength(100)]
    public string LastName { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(20)]
    public string SAPNo { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(150)]
    public string Position { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(150)]
    public string EmailAddress { get; set; }
    [Required]
    public Int64 DepartmentId { get; set; }
    [Required]
    public int LocationId { get; set; }
    [Required]
    public int RoleId { get; set; }
    [Column(TypeName = "VARCHAR")]
    [StringLength(20)]
    public string CreatedBy { get; set; }
    public string CreatedDate { get; set; }
    public bool IsActive { get; set; } = true;
}

2 - 我在继承 IdentityRole 的 ApplicationDbContext.cs 中添加了 ApplicationRole 类。留空

public class ApplicationRole : IdentityRole<Int64>
{
}

3 - 我将 ApplicationDbContext.cs 中的 IdentityDbContext 类从 IdentityDbContext&lt;ApplicationUser&gt; 更改为 IdentityDbContext&lt;ApplicationUser,ApplicationRole, Int64&gt;

public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole, Int64>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

这是 ApplicationDbContext.cs 的完整代码:

public class ApplicationUser : IdentityUser<Int64>
{
    [Required]
    [StringLength(100)]
    public string FirstName { get; set; }
    [Required]
    [StringLength(100)]
    public string LastName { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(20)]
    public string SAPNo { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(150)]
    public string Position { get; set; }
    [Required]
    [Column(TypeName = "VARCHAR")]
    [StringLength(150)]
    public string EmailAddress { get; set; }
    [Required]
    public Int64 DepartmentId { get; set; }
    [Required]
    public int LocationId { get; set; }
    [Required]
    public int RoleId { get; set; }
    [Column(TypeName = "VARCHAR")]
    [StringLength(20)]
    public string CreatedBy { get; set; }
    public string CreatedDate { get; set; }
    public bool IsActive { get; set; } = true;
}

public class ApplicationRole : IdentityRole<Int64>
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole, Int64>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

【讨论】:

    【解决方案2】:

    为什么不用 new 关键字覆盖它。我不确定它是否会起作用,但我假设 EF Core 将使用新 ID 作为主键

    public class IdentityUser
    {
         public string Id { get; set; }
    }
    
    public class ApplicationUser : IdentityUser 
    {
          public new long Id { get; set; }
    }
    

    【讨论】:

    • 试图迁移这个并在数据库中更新,但没有任何改变
    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多