在.net Core中使用代码优先迁移将ASPNetUsers主键数据类型从nvarchar更新为bigint.网5).



这是我的ApplicationDbContext.cs代码,将其他字段添加到ASPNetUsers表。我注意到数据库中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)
{
}
}

我在YouTube上看到了一个有关EF Core Migrations的视频,并能够将其应用到我的代码中。

1 -我将ApplicationDbContext.cs中的ApplicationUser类从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 -我在ApplicationDbContext.cs中添加了ApplicationRole类,它继承了IdentityRole。留空

public class ApplicationRole : IdentityRole<Int64>
{
}

3 -我改变了IdentityDbContext类在ApplicationDbContext.cs从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)
{
}
}

为什么不用新的关键字覆盖它呢?我不确定它是否会起作用,但我认为EF Core将使用新的ID作为主键

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

相关内容

  • 没有找到相关文章

最新更新