Here's my ApplicationDbContext.cs code that adds other fields to ASPNetUsers table. I noticed that the Identity generated tables in the database set the primary key, Id, as nvarchar (450). How will I change ASPNetUsers' Id to bigint (Int64) via code first migration approach?
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)
{
}
}
CodePudding user response:
I saw a video on YouTube related to EF Core Migrations and was able to apply it in my codes.
1 - I changed ApplicationUser class in ApplicationDbContext.cs from ApplicationUser : IdentityUser
to ApplicationUser : IdentityUser<Int64>
so that PK will update to bigint in database. You can set it to int if you want the PK to be int only in database.
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 - I added ApplicationRole class in ApplicationDbContext.cs that inherits IdentityRole. Left it empty
public class ApplicationRole : IdentityRole<Int64>
{
}
3 - I changed the IdentityDbContext class in ApplicationDbContext.cs from IdentityDbContext<ApplicationUser>
to IdentityDbContext<ApplicationUser,ApplicationRole, Int64>
public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole, Int64>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Here's the whole code of 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)
{
}
}
CodePudding user response:
Why don't you override it with the new keyword. I'm not sure if it will work, but I assume EF Core will use the new ID as the primary key
public class IdentityUser
{
public string Id { get; set; }
}
public class ApplicationUser : IdentityUser
{
public new long Id { get; set; }
}