public IHttpActionResult PutStudent(string id, Student student) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != student.Id) { return(BadRequest()); } db.Entry(student).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void Create() { new DefaultTeamCreator(_context).Create(); new DefaultRoleAndUserCreator(_context).Create(); _context.SaveChanges(); }
// ------------------------------------------------------------------- // TODO: Is there a better way to handle the transaction contained // within the Create method? Is db.SaveChanges() actually sufficient? // // See https://docs.microsoft.com/en-us/ef/core/saving/transactions // -------------------------------------------------------------------- public User Create(User user, string password) { if (string.IsNullOrWhiteSpace(password)) { throw new ApplicationException("Invalid password."); } using (var transaction = db.Database.BeginTransaction()) { if (db.User.Any(u => u.Username == user.Username)) { throw new ApplicationException($"Username: {user.Username} is already taken"); } // Pass by reference. byte[] salt, hash; SecurityUtil.Hash(password, out salt, out hash); user.PasswordSalt = salt; user.PasswordHash = hash; db.User.Add(user); db.SaveChanges(); transaction.Commit(); return(user); } }
public void Create() { var defaultTeam = _context.Teams.FirstOrDefault(e => e.DisplayName == TeamManager.DefaultTeamName); if (defaultTeam == null) { defaultTeam = new Team { DisplayName = TeamManager.DefaultTeamName, Code = "0000.0000.0000", ParentId = Guid.Empty }; _context.Teams.Add(defaultTeam); _context.SaveChanges(); } }
private void CreateHostRoleAndUsers() { var adminRole = _context.Roles.FirstOrDefault(r => r.Name == StaticRoleNames.Host.Admin); if (adminRole == null) { adminRole = _context.Roles.Add(new Role { Name = StaticRoleNames.Host.Admin, DisplayName = StaticRoleNames.Host.Admin, IsStatic = true }); _context.SaveChanges(); // todo: 增加User以外所有 var permissions = PermissionFinder .GetAllPermissions(new UserAuthorizationProvider(), new PermissionAuthorizationProvider(), new PostAuthorizationProvider(), new ProjectAuthorizationProvider(), new TaskAuthorizationProvider(), new RoleAuthorizationProvider(), new TeamAuthorizationProvider()) .ToList(); foreach (var permission in permissions) { if (!string.IsNullOrEmpty(permission.Name)) { _context.PermissionSettings.Add( new PermissionSetting { Name = permission.Name, IsGranted = true, RoleId = adminRole.Id, }); } } _context.SaveChanges(); } var defaultTeam = _context.Teams.FirstOrDefault(t => t.DisplayName == TeamManager.DefaultTeamName); if (defaultTeam == null) { defaultTeam = new Team { DisplayName = TeamManager.DefaultTeamName, Code = "0000.0000.0000", ParentId = Guid.Empty }; _context.Teams.Add(defaultTeam); _context.SaveChanges(); } var adminUser = _context.Users.FirstOrDefault(u => u.UserName == User.AdminUserName); if (adminUser == null) { adminUser = new User { UserName = User.AdminUserName, Name = "admin", Surname = "Administrator", EmailAddress = "*****@*****.**", IsEmailConfirmed = true, Password = new PasswordHasher().HashPassword("Mit000"), TeamId = defaultTeam.Id }; _context.Users.Add(adminUser); _context.SaveChanges(); _context.UserRoles.Add(new UserRole(adminUser.Id, adminRole.Id)); _context.SaveChanges(); } }
public virtual bool Save() { var changes = Db.SaveChanges(); return(changes > 0); }