public virtual bool RollbackTransaction(ZOperationResult operationResult) { try { if (PersistenceHelper.IsTransaction) { if (Transaction != null) { if (Transaction.UnderlyingTransaction != null && Transaction.UnderlyingTransaction.Connection != null && Transaction.UnderlyingTransaction.Connection.State == ConnectionState.Open) { Transaction.Rollback(); } Transaction.Dispose(); Transaction = null; } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public virtual bool Update(ZOperationResult operationResult, TEntity entity) { try { if (entity.BeforeUpdate(operationResult)) { if (BeforeUpdate(operationResult, entity)) { //if (UnitOfWork.BeforeUpdate(operationResult, entity)) { //Set.Attach(entity); //Context.Entry(entity).State = EntityState.Modified; Set.AddOrUpdate(entity); // System.Data.Entity.Migrations if (entity.AfterUpdate(operationResult)) { AfterUpdate(operationResult, entity); //{ // UnitOfWork.AfterUpdate(operationResult, entity); //} } } } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public virtual bool Save(ZOperationResult operationResult) { try { Context.SaveChanges(); } catch (DbEntityValidationException exception) { foreach (DbEntityValidationResult validationErrors in exception.EntityValidationErrors) { foreach (DbValidationError validationError in validationErrors.ValidationErrors) { operationResult.AddOperationError("", validationError.ErrorMessage, new List <string>() { validationError.PropertyName }); } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public virtual bool Create(ZOperationResult operationResult, TEntity entity) { try { if (UnitOfWork.BeforeCreate(operationResult, entity)) { if (BeforeCreate(operationResult, entity)) { object id = GetNextSequence(); if (id != null) { (entity as ZDataBase).SetId(new object[] { id }); } Set.Add(entity); if (AfterCreate(operationResult, entity)) { UnitOfWork.AfterCreate(operationResult, entity); } } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public virtual bool Delete(ZOperationResult operationResult, TEntity entity) { try { string predicate = Profile.LINQWhere; Expression <Func <TEntity, bool> > where = System.Linq.Dynamic.DynamicExpression.ParseLambda <TEntity, bool>(predicate, GetIds(entity)); entity = Set.AsQueryable <TEntity>() .Where(where) .AsNoTracking() .FirstOrDefault(); //entity = GetById(GetIds(entity)); if (entity != null) { if (entity.BeforeDelete(operationResult)) { if (BeforeDelete(operationResult, entity)) { //if (UnitOfWork.BeforeDelete(operationResult, entity)) { Context.Entry(entity).State = EntityState.Deleted; //Set.Attach(entity); //Set.Remove(entity); if (entity.AfterDelete(operationResult)) { AfterDelete(operationResult, entity); //{ // UnitOfWork.AfterDelete(operationResult, entity); //} } } } } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public override bool Update(ZOperationResult operationResult, User entity) //public override async void Update(ZOperationResult operationResult, User entity) { try { ApplicationUser user = IdentityHelperEF.UserManager.FindById(entity.Id); user.Email = entity.Email; IdentityResult validEmail = new IdentityResult(); //validEmail = await IdentityHelperEF.UserManager.UserValidator.ValidateAsync(user); if (!validEmail.Succeeded) { operationResult.ParseIdentityResult(validEmail); } IdentityResult validPassword = new IdentityResult(); if (!String.IsNullOrEmpty(entity.PasswordHash)) { //validPassword = await IdentityHelperEF.UserManager.PasswordValidator.ValidateAsync(entity.PasswordHash); if (validPassword.Succeeded) { user.PasswordHash = IdentityHelperEF.UserManager.PasswordHasher.HashPassword(entity.PasswordHash); } else { operationResult.ParseIdentityResult(validPassword); } } if (validEmail.Succeeded && validPassword.Succeeded) { IdentityResult identityResult = IdentityHelperEF.UserManager.Update(user); if (!identityResult.Succeeded) { operationResult.ParseIdentityResult(identityResult); } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public virtual bool BeginTransaction(ZOperationResult operationResult, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) { try { if (PersistenceHelper.IsTransaction) { if (Transaction == null || Transaction.UnderlyingTransaction.Connection == null) { Transaction = Context.Database.BeginTransaction(isolationLevel); } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public override bool Update(ZOperationResult operationResult, Role entity) { try { ApplicationRole role = IdentityHelperEF.RoleManager.FindById(entity.Id); role.Name = entity.Name; IdentityResult identityResult = IdentityHelperEF.RoleManager.Update(role); if (!identityResult.Succeeded) { operationResult.ParseIdentityResult(identityResult); } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public virtual bool RollbackTransaction(ZOperationResult operationResult, bool isTransaction = true) { try { if (isTransaction && PersistenceHelper.IsTransaction) { if (Transaction != null) { Transaction.Rollback(); } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public override bool Create(ZOperationResult operationResult, User entity) { try { ApplicationUser user = new ApplicationUser { UserName = entity.UserName, Email = entity.Email, EmailConfirmed = true }; IdentityResult identityResult = IdentityHelperEF.UserManager.Create(user, entity.PasswordHash); if (!identityResult.Succeeded) { operationResult.ParseIdentityResult(identityResult); } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public override bool Create(ZOperationResult operationResult, Role entity) { try { ApplicationRole role = new ApplicationRole { Name = entity.Name }; IdentityResult identityResult = IdentityHelperEF.RoleManager.Create(role); if (!identityResult.Succeeded) { operationResult.ParseIdentityResult(identityResult); } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public override bool Create(ZOperationResult operationResult, User entity) { try { if (!entity.LockoutEnabled) { entity.LockoutEndDateUtc = null; } else { if (entity.LockoutEndDateUtc == null) { entity.LockoutEndDateUtc = DateTime.Now.AddYears(1); } } ApplicationUser user = new ApplicationUser { UserName = entity.UserName, Email = entity.Email, EmailConfirmed = true, LockoutEnabled = entity.LockoutEnabled, LockoutEndDateUtc = entity.LockoutEndDateUtc }; IdentityResult identityResult = IdentityHelperEF.UserManager.Create(user, entity.PasswordHash); if (!identityResult.Succeeded) { operationResult.ParseIdentityResult(identityResult); } else { entity.Id = user.Id; } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public override bool Delete(ZOperationResult operationResult, User entity) { try { ApplicationUser user = IdentityHelperEF.UserManager.FindById(entity.Id); if (user != null) { IdentityResult identityResult = IdentityHelperEF.UserManager.Delete(user); if (!identityResult.Succeeded) { operationResult.ParseIdentityResult(identityResult); } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
public virtual bool Delete(ZOperationResult operationResult, TEntity entity) { try { if (UnitOfWork.BeforeDelete(operationResult, entity)) { if (BeforeDelete(operationResult, entity)) { Context.Entry(entity).State = EntityState.Deleted; if (AfterDelete(operationResult, entity)) { UnitOfWork.AfterDelete(operationResult, entity); } } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }
private static void PersistenceChinookTransactionDemo(bool isCommit = true) { if (isCommit) { Console.WriteLine("\nPersistence Chinook Transaction Demo with Commit"); } else { Console.WriteLine("\nPersistence Chinook Transaction Demo with Rollback"); } var container = new UnityContainer(); UnityHelper.RegisterMappings(container); IUnitOfWork unitOfWork = (IUnitOfWork)container.Resolve <IChinookUnitOfWork>(); IGenericRepository <Artist> repository = unitOfWork.GetRepository <Artist>(); ZOperationResult operationResult = new ZOperationResult(); try { unitOfWork.BeginTransaction(operationResult); Artist artist; artist = new Artist(0, "Artist 1"); if (repository.Create(operationResult, artist) && unitOfWork.Save(operationResult)) { artist = new Artist(0, "Artist 2"); if (repository.Create(operationResult, artist) && unitOfWork.Save(operationResult)) { artist = new Artist(0, "Artist 3"); if (repository.Create(operationResult, artist)) { unitOfWork.Save(operationResult); } } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); unitOfWork.RollbackTransaction(operationResult); } finally { if (isCommit) { unitOfWork.CommitTransaction(operationResult); } else { unitOfWork.RollbackTransaction(operationResult); } } if (!operationResult.Ok) { Console.WriteLine("\n"); Console.WriteLine(operationResult.Text); } }
public override bool Update(ZOperationResult operationResult, User entity) //public override async void Update(ZOperationResult operationResult, User entity) { try { if (!entity.LockoutEnabled) { entity.LockoutEndDateUtc = null; } else { if (entity.LockoutEndDateUtc == null) { entity.LockoutEndDateUtc = DateTime.Now.AddYears(1); } } ApplicationUser user = IdentityHelperEF.UserManager.FindById(entity.Id); user.LockoutEnabled = entity.LockoutEnabled; user.LockoutEndDateUtc = entity.LockoutEndDateUtc; user.Email = entity.Email; //IdentityResult validEmail = new IdentityResult(); //validEmail = await IdentityHelperEF.UserManager.UserValidator.ValidateAsync(user); //if (!validEmail.Succeeded) //{ // operationResult.ParseIdentityResult(validEmail); //} /* * //IdentityResult validPassword = new IdentityResult(); * if (!String.IsNullOrEmpty(entity.PasswordHash)) * { * //validPassword = await IdentityHelperEF.UserManager.PasswordValidator.ValidateAsync(entity.PasswordHash); * //if (validPassword.Succeeded) * { * user.PasswordHash = IdentityHelperEF.UserManager.PasswordHasher.HashPassword(entity.PasswordHash); * } * //else * //{ * // operationResult.ParseIdentityResult(validPassword); * //} * } */ //if (validEmail.Succeeded && validPassword.Succeeded) { IdentityResult identityResult = IdentityHelperEF.UserManager.Update(user); if (!identityResult.Succeeded) { operationResult.ParseIdentityResult(identityResult); } } } catch (Exception exception) { operationResult.ParseExceptionEntityFramework(exception); } return(operationResult.Ok); }