使用 .Net Core (.Net 5) 中的代码优先迁移将 ASPNetUsers 主键数据类型从 nvarchar 更新为 bigint
Updating ASPNetUsers primary key data type from nvarchar to bigint using code first migration in .Net Core (.Net 5)
这是我的 ApplicationDbContext.cs 代码,用于将其他字段添加到 ASPNetUsers table。我注意到数据库中生成的身份 tables 将主键 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)
{
}
}
为什么不用 new 关键字覆盖它。我不确定它是否有效,但我假设 EF Core 将使用新 ID 作为主键
public class IdentityUser
{
public string Id { get; set; }
}
public class ApplicationUser : IdentityUser
{
public new long Id { get; set; }
}
我在 YouTube 上看到了一个与 EF Core 迁移相关的视频,并且能够将其应用到我的代码中。
1 - 我将 ApplicationDbContext.cs 中的 ApplicationUser class 从 ApplicationUser : IdentityUser
更改为 ApplicationUser : IdentityUser<Int64>
以便 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 class。留空
public class ApplicationRole : IdentityRole<Int64>
{
}
3 - 我将 ApplicationDbContext.cs 中的 IdentityDbContext class 从 IdentityDbContext<ApplicationUser>
更改为 IdentityDbContext<ApplicationUser,ApplicationRole, Int64>
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)
{
}
}
这是我的 ApplicationDbContext.cs 代码,用于将其他字段添加到 ASPNetUsers table。我注意到数据库中生成的身份 tables 将主键 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)
{
}
}
为什么不用 new 关键字覆盖它。我不确定它是否有效,但我假设 EF Core 将使用新 ID 作为主键
public class IdentityUser
{
public string Id { get; set; }
}
public class ApplicationUser : IdentityUser
{
public new long Id { get; set; }
}
我在 YouTube 上看到了一个与 EF Core 迁移相关的视频,并且能够将其应用到我的代码中。
1 - 我将 ApplicationDbContext.cs 中的 ApplicationUser class 从 ApplicationUser : IdentityUser
更改为 ApplicationUser : IdentityUser<Int64>
以便 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 class。留空
public class ApplicationRole : IdentityRole<Int64>
{
}
3 - 我将 ApplicationDbContext.cs 中的 IdentityDbContext class 从 IdentityDbContext<ApplicationUser>
更改为 IdentityDbContext<ApplicationUser,ApplicationRole, Int64>
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)
{
}
}