Esempio n. 1
0
        public async Task SeedAsync()
        {
            _context.Database.EnsureCreated();

            StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new StoreUser()
                {
                    Email    = "*****@*****.**",
                    UserName = "******"
                };

                var result = await _userManager.CreateAsync(user, "Password!1");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Couldn't create new user in seeder");
                }
            }

            IdentityRole role = await _roleManager.FindByNameAsync("Administrators");

            if (role == null)
            {
                role = new IdentityRole()
                {
                    Name = "Administrators"
                };

                var roleResult = await _roleManager.CreateAsync(role);

                if (roleResult != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Couldn't create new role in seeder");
                }
            }

            bool assignmentTest = await _userManager.IsInRoleAsync(user, "Administrators");

            if (!assignmentTest)
            {
                var assignmentResult = await _userManager.AddToRoleAsync(user, "Administrators");

                if (assignmentResult != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Couldn't assign Administrators role to user in seeder");
                }
            }

            if (!_context.Dogs.Any())
            {
                var filepath = Path.Combine(_hosting.ContentRootPath, "Data/Dogs.json");
                var jsonDogs = File.ReadAllText(filepath);
                var dogs     = JsonConvert.DeserializeObject <IEnumerable <Dog> >(jsonDogs);
                _context.Dogs.AddRange(dogs);

                filepath = Path.Combine(_hosting.ContentRootPath, "Data/AdoptionStories.json");
                var jsonAdoptions = File.ReadAllText(filepath);
                var adoptions     = JsonConvert.DeserializeObject <IEnumerable <AdoptionStory> >(jsonAdoptions);
                _context.AdoptionStories.AddRange(adoptions);

                _context.SaveChanges();
            }
        }
Esempio n. 2
0
 public bool Save()
 {
     return(_context.SaveChanges() >= 0);
 }