Skip to content

Instantly share code, notes, and snippets.

@gmartinezsan
Last active April 19, 2018 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmartinezsan/e32fbb00e5c777fb6a71415b686e8695 to your computer and use it in GitHub Desktop.
Save gmartinezsan/e32fbb00e5c777fb6a71415b686e8695 to your computer and use it in GitHub Desktop.
using BooksWebApi.Entities;
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BooksWebApi.Data
{
public static class DataInitializer
{
static Category _category = new Category { Description = "Science", Books = new List<Book>() };
static List<Book> _sample = new List<Book>
{
new Book()
{
Name = "A science book",
AuthorName = "A good author",
Edition = 1,
PublicationDate = new DateTime(2010, 5 , 1),
ISDN = "1111111111111111",
Category = _category
}
};
internal static void Seed(UserManager<User> userMgr, RoleManager<IdentityRole> roleMgr, BooksCatalogDbContext ctx)
{
ctx.Database.EnsureCreated();
if (userMgr.FindByNameAsync("myapiuser").Result == null)
{
if (!roleMgr.RoleExistsAsync("Administrator").Result)
{
IdentityRole role = new IdentityRole();
role.Name = "Administrator";
IdentityResult result = roleMgr.CreateAsync(role).Result;
}
var user = new User()
{
UserName = "myapiuser",
FirstName = "myapiuser",
LastName = "webapi",
Email = "gmartinezsan@gmail.com"
};
var userResult = userMgr.CreateAsync(user, "MyP4ssw0rd!").Result;
if (userResult.Succeeded)
{
userMgr.AddToRoleAsync(user, "Administrator").Wait();
}
else
{
throw new InvalidOperationException("Failed to build user or role");
}
ctx.SaveChanges();
}
if (!ctx.Books.Any() && !ctx.Categories.Any())
{
ctx.Add(_category);
ctx.AddRange(_sample);
ctx.SaveChanges();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment