這是我的ApplicationDbContext.cs代碼,在ASPNetUsers表中添加了其他欄位。我注意到資料庫中的個人資訊生成表將主鍵Id設定為nvarchar (450)。我將如何通過代碼先行的遷移方法將ASPNetUsers的Id改為bigint(Int64)?
public class ApplicationUser : IdentityUser
{
[需要] 。
[StringLength(100)]
public string FirstName { get; set; }
[必填] [StringLength(100)] public string FirstName { get; set; }
[字串長度(100)]
public string LastName { get; set; }
[必須的] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")] 欄目
[字串長度(20)]
公共字串SAPNo { get; set; }
[需要] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")
[StringLength(150)]
public string Position { get; set; }
[需要] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")
[StringLength(150)]
public string EmailAddress { get; set; }
[需要] [Column(TypeName = "VARCHAR") ] [StringLength(150)] public string EmailAddress { get; set; }
public Int64 DepartmentId { get; set; }
[必須的] Public int LocationId { get; set; }
公共int LocationId { get; set; }
[必須的]
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)
{
}
}
uj5u.com熱心網友回復:
我在YouTube上看到一個與EF Core Migrations有關的視頻,并且能夠在我的代碼中應用它。
1 - 我將ApplicationDbContext.cs中的ApplicationUser類從ApplicationUser : IdentityUser改為ApplicationUser : IdentityUser<Int64>,這樣PK將在資料庫中更新為大數。如果你想讓PK在資料庫中只成為int,你可以把它設定為int。
public class ApplicationUser : IdentityUser<Int64>
{
[必須的]
[StringLength(100)]
public string FirstName { get; set; }
[必填] [StringLength(100)] public string FirstName { get; set; }
[字串長度(100)]
public string LastName { get; set; }
[必須的] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")] 欄目
[字串長度(20)]
公共字串SAPNo { get; set; }
[需要] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")
[StringLength(150)]
public string Position { get; set; }
[需要] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")
[StringLength(150)]
public string EmailAddress { get; set; }
[需要] [Column(TypeName = "VARCHAR") ] [StringLength(150)] public string EmailAddress { get; set; }
public Int64 DepartmentId { get; set; }
[必須的] Public int LocationId { get; set; }
公共int LocationId { get; set; }
[必須的]
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 - 我在ApplicationDbContext.cs中添加了ApplicationRole類,它繼承了IdentityRole。讓它為空
public class ApplicationRole : IdentityRole<Int64>
{
}
3 - 我把ApplicationDbContext.cs中的IdentityDbContext類從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>
{
[必須的]
[StringLength(100)]
public string FirstName { get; set; }
[必填] [StringLength(100)] public string FirstName { get; set; }
[字串長度(100)]
public string LastName { get; set; }
[必須的] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")] 欄目
[字串長度(20)]
公共字串SAPNo { get; set; }
[需要] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")
[StringLength(150)]
public string Position { get; set; }
[需要] [Column(TypeName = "VARCHAR")
[Column(TypeName = "VARCHAR")
[StringLength(150)]
public string EmailAddress { get; set; }
[需要] [Column(TypeName = "VARCHAR") ] [StringLength(150)] public string EmailAddress { get; set; }
public Int64 DepartmentId { get; set; }
[必須的] Public int LocationId { get; set; }
公共int LocationId { get; set; }
[必須的]
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)
{
}
}
uj5u.com熱心網友回復:
你為什么不用新的關鍵字來覆寫它呢。我不確定它是否能作業,但我認為EF Core會使用新的ID作為主鍵
public class IdentityUser
{
public string Id { get; set; }
}
public class ApplicationUser : IdentityUser
{
public new long Id { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/311760.html
標籤:
