public void InitializePermissions(List <Permission> permissions) { _dbContext.RolePermissions.RemoveRange(_dbContext.RolePermissions.Where(rp => rp.RoleId == DefaultRoles.Admin.Id)); _dbContext.SaveChanges(); _dbContext.Permissions.RemoveRange(_dbContext.Permissions); _dbContext.SaveChanges(); _dbContext.AddRange(permissions); GrantAllPermissionsToAdminRole(permissions); _dbContext.SaveChanges(); }
public override void OnActionExecuted(ActionExecutedContext context) { var dataModificationRequestTypes = new[] { "post", "put", "delete" }; if (!dataModificationRequestTypes.Contains(context.HttpContext.Request.Method, StringComparer.InvariantCultureIgnoreCase) || context.Exception != null || !context.ModelState.IsValid) { return; } using (var transaction = _dbContext.Database.BeginTransaction()) { try { _dbContext.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public override void OnActionExecuted(ActionExecutedContext context) { var dataModificationRequestTypes = new[] { "post", "put", "delete" }; if (!dataModificationRequestTypes.Contains(context.HttpContext.Request.Method, StringComparer.InvariantCultureIgnoreCase) || context.Exception != null || !context.ModelState.IsValid) { return; } try { _dbContext.SaveChanges(); } catch (DbUpdateConcurrencyException dbUpdateConcurrencyException) { dbUpdateConcurrencyException.Entries.Single().Reload(); _dbContext.SaveChanges(); } }
public async void Should_Remove_User() { var testUser = await CreateAndGetTestUser(); var dbContextFromAnotherScope = TestServer.Host.Services.GetRequiredService <NucleusDbContext>(); var insertedTestUser = await dbContextFromAnotherScope.Users.FindAsync(testUser.Id); Assert.NotNull(insertedTestUser); Assert.Equal(1, insertedTestUser.UserRoles.Count); _userAppService.RemoveUser(insertedTestUser.Id); _dbContext.SaveChanges(); dbContextFromAnotherScope = TestServer.Host.Services.GetRequiredService <NucleusDbContext>(); var removedTestUser = await dbContextFromAnotherScope.Users.FindAsync(testUser.Id); var removedRoleMatches = dbContextFromAnotherScope.UserRoles.Where(rp => rp.UserId == testUser.Id); Assert.Null(removedTestUser); Assert.Equal(0, removedRoleMatches.Count()); }
public async void Should_Remove_Role() { var testRole = await CreateAndGetTestRole(); var dbContextFromAnotherScope = TestServer.Host.Services.GetRequiredService <NucleusDbContext>(); var insertedTestRole = await dbContextFromAnotherScope.Roles.FindAsync(testRole.Id); Assert.NotNull(insertedTestRole); Assert.Equal(1, insertedTestRole.RolePermissions.Count); _roleAppService.RemoveRole(insertedTestRole.Id); _dbContext.SaveChanges(); dbContextFromAnotherScope = TestServer.Host.Services.GetRequiredService <NucleusDbContext>(); var removedTestRole = await dbContextFromAnotherScope.Roles.FindAsync(testRole.Id); var removedPermissionMatches = dbContextFromAnotherScope.RolePermissions.Where(rp => rp.RoleId == testRole.Id); var removedUserMatches = dbContextFromAnotherScope.RolePermissions.Where(rp => rp.RoleId == testRole.Id); Assert.Null(removedTestRole); Assert.Equal(0, removedPermissionMatches.Count()); Assert.Equal(0, removedUserMatches.Count()); }
public override void OnActionExecuted(ActionExecutedContext context) { if (!context.HttpContext.Request.Method.Equals("Post", StringComparison.OrdinalIgnoreCase) || context.Exception != null || !context.ModelState.IsValid) { return; } using (var transaction = _dbContext.Database.BeginTransaction()) { try { _dbContext.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public bool SaveChanges() { return(_context.SaveChanges() >= 0); }