protected override void Seed(ThingWall.Data.DataContext context) { //PROTIP: tutaj mo¿na wype³niæ bazê danych wstêpnymi danymi //UWAGA: trzeba sprawdzaæ, czy dany rekord ju¿ istnieje, Seed uruchamiany jest po ka¿dej migracji var examples = new List <ExampleItem>() { new ExampleItem() { Name = "Test 1" }, new ExampleItem() { Name = "Test 2" }, new ExampleItem() { Name = "Test 3" } }; //PROTIP: mechanizm u¿ytkowników dostarza ASP.NET Identity //klasa UserManager pozwala trochê ³atwiej ich ogarn¹æ var userManager = new Microsoft.AspNet.Identity.UserManager <User>(new UserStore <User>(context)); var testUser = userManager.FindByEmail("*****@*****.**"); if (testUser == null) { var hasher = new PasswordHasher(); testUser = new User() { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PasswordHash = hasher.HashPassword("test"), Nick = "NowyNick" }; userManager.Create(testUser); } foreach (var example in examples) { if (!context.ExampleItems.Any(i => i.Name == example.Name)) { //PROTIP: ustawianie klucza obcego wystarczy do "zrobienia" relacji example.OwnerId = testUser.Id; context.ExampleItems.Add(example); } } //PROTIP: SaveChanges poza pêtl¹ wrzuca wszystkie zmiany w kolejce jednym zapytaniem context.SaveChanges(); }
public async Task <ActionResult> EditProfile(ApplicationUser _user) { ////get current user and update //var userCurnt = await UserManager.FindByIdAsync(User.Identity.GetUserId()); //userCurnt.Image = _user.Image; //userCurnt.UserName = _user.UserName; //userCurnt.Email = _user.Email; //var updateResult = await UserManager.UpdateAsync(user); //if (updateResult.Succeeded) //{ // //do something and return // return RedirectToAction("showprofile"); //} ////failed - do something else and return //return View(); if (!ModelState.IsValid) { return(View(_user)); } var userStore = new UserStore <ApplicationUser>(new ApplicationDbContext()); var appManager = new Microsoft.AspNet.Identity.UserManager <ApplicationUser>(userStore); var currentUser = appManager.FindByEmail(_user.Email); // here you can assign the updated values currentUser.Image = _user?.Image; currentUser.Email = _user?.Email; currentUser.UserName = _user?.UserName; // and rest fields are goes here await appManager.UpdateAsync(currentUser); var ctx = userStore.Context; ctx.SaveChanges(); // now you can redirect to some other method or-else you can return // to this view itself by returning the data return(RedirectToAction("showProfile")); }
protected override void Seed(DashoundCoachTravels.Models.ApplicationDbContext context) { var userManager = new Microsoft.AspNet.Identity.UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); //Creating new role types for accounts var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); IdentityResult roleResult; // Adding new role types named: Administrator, Employee, User if (!roleManager.RoleExists("Administrator")) { roleResult = roleManager.Create(new IdentityRole("Administrator")); } if (!roleManager.RoleExists("Employee")) { roleResult = roleManager.Create(new IdentityRole("Employee")); } if (!roleManager.RoleExists("User")) { roleResult = roleManager.Create(new IdentityRole("User")); } //Adding sample users: 1 for each role type testing only: Basic|Employee|Admin var sampleAccount = userManager.FindByEmail("*****@*****.**"); if (sampleAccount == null) { var hasher = new PasswordHasher(); sampleAccount = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PasswordHash = hasher.HashPassword("user"), Name = "Jon", Surname = "Doe", Town = "aaa", Street = "streetA", NumHouse = "1", NumFlat = "12", ZIPCode = "22-222", Country = "Uganda" }; userManager.Create(sampleAccount); userManager.AddToRole(sampleAccount.Id, "User"); context.SaveChanges(); } sampleAccount = userManager.FindByEmail("*****@*****.**"); if (sampleAccount == null) { var hasher = new PasswordHasher(); sampleAccount = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PasswordHash = hasher.HashPassword("employee"), Name = "Jonatan", Surname = "Doppey", Town = "bbb", Street = "streetB", NumHouse = "2", NumFlat = "186", ZIPCode = "66-666", Country = "Nigeria" }; userManager.Create(sampleAccount); userManager.AddToRole(sampleAccount.Id, "Employee"); context.SaveChanges(); } sampleAccount = userManager.FindByEmail("*****@*****.**"); if (sampleAccount == null) { var hasher = new PasswordHasher(); sampleAccount = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", EmailConfirmed = true, PasswordHash = hasher.HashPassword("admin"), Name = "Jonny", Surname = "Dope", Town = "ccc", Street = "streetC", NumHouse = "79", NumFlat = "6", ZIPCode = "99-971", Country = "Egypt" }; userManager.Create(sampleAccount); userManager.AddToRole(sampleAccount.Id, "Administrator"); context.SaveChanges(); } // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. }