Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DbInitializer.Seed(serviceProvider.GetRequiredService<AppDbContext>());
- // IServiceProvider serviceProvider
- using Microsoft.AspNetCore.Identity;
- using Models;
- using System;
- using System.Linq;
- namespace Data.Seed
- {
- public class DbInitializer
- {
- private static string adminRoleId = "e8b6fc47-ad33-486e-ac87-37d639562e71";
- private static string userRoleId = "18b6fc47-ad33-486e-ac87-37d639562e71";
- private static string userId = "08b6fc47-ad33-486e-ac87-37d639562e71";
- public static void Seed(AppDbContext context)
- {
- if( !context.Roles.Any())
- {
- context.AddRange
- (
- new IdentityRole { Id = adminRoleId, Name = "Administrator", NormalizedName = "Administrator".ToUpper() },
- new IdentityRole { Id= userRoleId, Name = "User", NormalizedName = "User".ToUpper() }
- );
- }
- if (!context.Posts.Any())
- {
- context.AddRange
- (
- new Post
- {
- Title = "New Post",
- ImageUrl = "image",
- Rating = 3,
- CreatedAt = DateTime.UtcNow
- }
- );
- }
- if (!context.Users.Any())
- {
- var user = new User
- {
- Id = userId,
- UserName = "[email protected]",
- Email = "[email protected]",
- SecurityStamp = string.Empty,
- };
- user.PasswordHash = PassGenerate(user);
- context.AddRange(user);
- }
- if (!context.UserRoles.Any())
- {
- context.AddRange
- (
- new IdentityUserRole<string> { UserId = "08b6fc47-ad33-486e-ac87-37d639562e71", RoleId = "e8b6fc47-ad33-486e-ac87-37d639562e71" }
- );
- }
- context.SaveChanges();
- }
- public static string PassGenerate(User user)
- {
- var passHash = new PasswordHasher<User>();
- return passHash.HashPassword(user, "Administrator9#");
- }
- }
- }
Add Comment
Please, Sign In to add comment