protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity <User>() .HasMany(e => e.Roles) .WithOne() .HasForeignKey(e => e.UserId) .IsRequired() .OnDelete(DeleteBehavior.Cascade); modelBuilder.Entity <User>(entity => { entity.HasKey(r => new { r.Id }); entity.ToTable("AspNetUsers"); }); modelBuilder.Entity <IdentityRole <Guid> >(entity => { entity.HasKey(r => new { r.Id }); entity.ToTable("AspNetRoles"); }); modelBuilder.Entity <IdentityUserClaim <Guid> >(entity => { entity.HasKey(r => new { r.Id }); entity.ToTable("AspNetUserClaims"); }); modelBuilder.Entity <IdentityUserRole <Guid> >(entity => { entity.HasKey(r => new { r.UserId, r.RoleId }); entity.ToTable("AspNetUserRoles"); }); modelBuilder.Entity <IdentityUserLogin <Guid> >(entity => { entity.HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId }); entity.ToTable("AspNetUserLogins"); }); modelBuilder.Entity <IdentityUserToken <Guid> >(entity => { entity.HasKey(r => r.UserId); entity.ToTable("AspNetUserTokens"); }); UserSeedData.Initialize(modelBuilder); BlogSeedData.Initialize(modelBuilder); PostSeedData.Initialize(modelBuilder); }
public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <CMSContext>(); context.Database.Migrate(); UserSeedData.Initialize(services).Wait(); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred adding users."); } } host.Run(); }