public int Commit(bool isCommitedDbTransaction = true) { if (transaction != null) { if (isCommitedDbTransaction) { transaction.Commit(); transaction.Dispose(); return(0); } else { context.ChangeTracker.DetectChanges(); return(context.SaveChanges()); } } context.ChangeTracker.DetectChanges(); return(context.SaveChanges()); }
internal int Insert(TIdentityUser user) { int result = -1; try { User newUser = new User { UserName = user.UserName, PasswordHash = user.PasswordHash, SecurityStamp = user.SecurityStamp, Email = user.Email, EmailConfirmed = user.EmailConfirmed, MobileNumber = user.PhoneNumber, MobileNumberConfirmed = user.PhoneNumberConfirmed, LockoutEnabled = user.LockoutEnabled, LockoutEndDateUtc = user.LockoutEndDateUtc, AccessFailedCount = user.AccessFailedCount, Name = user.Name, LastName = user.LastName }; foreach (var item in user.Roles) { newUser.UserRoles.Add(new UserRole { RoleId = item.RoleId, UserId = item.UserId }); } foreach (var item in user.Claims) { newUser.UserClaims.Add(new UserClaim { ClaimType = item.ClaimType, ClaimValue = item.ClaimValue }); } _databaseContext.Users.Add(newUser); result = _databaseContext.SaveChanges(); user.Id = newUser.Id; } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { StringBuilder message = new StringBuilder(); foreach (var validationErrors in ex.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { message.Append(string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage)); // raise a new exception nesting // the current instance as InnerException } } throw new Exception(message.ToString(), ex); } return(result); }