我是 Asp.Net Core Identity 和 EF 迁移的新手。我使用代码优先方法在数据库中创建了与 Identity 相关的表。 AspNetUsers 表有一个名为 Email 的字段。我想将其重命名为 EmailAddress。所以在我的 dbContext 对象中我有以下代码。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("dbo");
modelBuilder.Entity<ApplicationUser>().Property(p =>
p.Email).HasColumnName("EmailAddress");
}
然后,我运行迁移,如下所示: PM> add-migration renameColumn -Context myDbContext PM> 更新数据库 renameColumn -Context myDbContext
运行后,当我检查我的 AspNetUsers 表时,该字段仍为电子邮件,并且没有更改为电子邮件地址。当我在 OnModelCreating() 函数上放置断点时,它永远不会到达它。运行迁移时我也没有收到任何错误。这应该是一个简单的过程。不知道为什么它不起作用!
首先确保将 OnModelCreating 放入用于身份的正确 dbcontxt 中。
我已经用身份用户的默认身份dbcontext尝试过,它有效。
运行 add-migration 命令后,它生成了如下迁移代码:
我的dbcontext代码:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("dbo");
modelBuilder.Entity<IdentityUser>().Property(p =>
p.Email).HasColumnName("EmailAddress");
}
}
结果: