【问题标题】:EF Core 2.1 Identity tables empty after HasData() seedingEF Core 2.1 身份表在 HasData() 播种后为空
【发布时间】:2018-11-20 01:02:28
【问题描述】:

我正在尝试使用 EF Core 2.1 HasData 模式播种我的身份表,代码执行但没有任何内容插入到表中。
我的 DBContext 类代码:

public partial class IdentityDBContext : IdentityDbContext<IdentityUser>
{
    public IdentityDBContext(DbContextOptions<IdentityDBContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        IdentityRole RootRole = new IdentityRole 
        { 
            Id = Guid.NewGuid().ToString(), 
            Name = IdentityDBContext.RoleTypes.RootAdmin, 
            NormalizedName = "Root Admin" 
        };
        builder.Entity<IdentityRole>().HasData(RootRole);

        IdentityUser AdminUser = new IdentityUser 
        { 
            Id = Guid.NewGuid().ToString(), 
            UserName = "m@soze.biz", 
            Email = "m@soze.biz", 
            NormalizedUserName = "m@soze.biz".ToUpper(), 
            NormalizedEmail = "m@soze.biz".ToUpper(), 
            EmailConfirmed = true 
        };
        builder.Entity<IdentityUser>().HasData(AdminUser);

        builder.Entity<IdentityUserRole<string>>().HasData(new IdentityUserRole<string> 
        { 
            UserId = AdminUser.Id, 
            RoleId = RootRole.Id 
        });
    }
}

在我拥有的 Configure 方法中的 Startup.cs 中:

using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
    var IdentityContext = serviceScope.ServiceProvider.GetRequiredService<IdentityDBContext>();
    IdentityContext.Database.Migrate();
}

我可以无异常地单步执行OnModelCreating 代码,但是没有记录添加到表中,AspNetUsersAspNetRoles 都是空的。

关于为什么这不起作用的任何建议?

【问题讨论】:

  • 添加 has 数据代码后是否创建了新的 EF 迁移?
  • 我确实在添加 HasData 代码后尝试添加迁移,但似乎我没有重新运行应用程序来推出该迁移。我知道这是我想念的简单的东西,谢谢阿曼!万一其他人遇到这个问题并遇到同样的问题,我在包管理器中运行“Add-Migration Update-Database -Context IdentityDBContext”,然后执行应用程序并将迁移推送到数据库。

标签: c# asp.net-core asp.net-identity ef-core-2.1


【解决方案1】:

对于您的问题,是Migrate 只应用了待处理的迁移,更改IdentityDBContext 后它不会检测到更改。

当您使用Identity 创建项目时,它会自动创建第一个迁移,然后即使您更改了IdentityDBContextMigrate 只会应用第一个迁移而无需您进行其他操作。

这里有两个选择:

  • 对于第一次初始化数据库,您可以尝试下面的代码,它将为数据提供种子。

            using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
        {
            var IdentityContext = serviceScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
            IdentityContext.Database.EnsureCreated();
        }
    
  • 第二个选项是您已经尝试过了。在运行 Migrate 之前,运行 Add-Migration 以生成新更改的迁移。

【讨论】:

    【解决方案2】:

    根据 Tao 的回答,如果您使用 Visual Studio 模板创建项目并在那里添加身份验证作为选项,则迁移将已经添加。 “HasData”方法不会在“Update-Database”阶段执行,它会在“Add-Migration”阶段执行并添加到您的迁移中。 您需要删除“Migrations”文件夹,然后在包命令窗口中运行“Add-Migration CreateIdentitySchema”,以便将种子数据应用于迁移。 然后,Update-Database 将从新迁移中播种您的数据。 这让我在一个星期天疯狂了 3 个小时。

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 2021-09-08
      相关资源
      最近更新 更多