/// <summary> /// [Chuc.Nguyen] - Xóa một đối tượng /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="id"></param> /// <returns></returns> public string Delete <TEntity>(Guid ID) where TEntity : class { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); try { //string tableName = typeof(TEntity).Name.Replace("Entity", ""); string tableName = typeof(TEntity).Name; tableName = tableName.Substring(0, tableName.Length - 6); Type dbEntityType = LibraryService.GetEntityType(tableName); object dbEntity = unitOfWork.CreateQueryable(Guid.Empty, dbEntityType, Constant.ID + " = @0", ID).FirstOrDefault(); unitOfWork.RemoveObject(dbEntityType, dbEntity); var status = unitOfWork.SaveChanges(Guid.Empty); if (status == DataErrorCode.Locked) { return(NotificationType.Locked.ToString()); } else { return(NotificationType.Success.ToString()); } } catch (Exception ex) { return(NotificationType.Error + "," + ex.Message); } } }
/// <summary> /// [HieuVan] - Chuyển đổi trạng thái của một đối tượng Status /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="id"></param> /// <returns></returns> public string UpdateStatus <TEntity>(Guid ID, string type) where TEntity : class { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repo = new CustomBaseRepository <TEntity>(unitOfWork); try { List <object> listParamater = new List <object>(); string tableName = typeof(TEntity).Name; tableName = tableName.Substring(0, tableName.Length - 6); Type dbEntityType = LibraryService.GetEntityType(tableName); object dbEntity = unitOfWork.CreateQueryable(Guid.Empty, dbEntityType, Constant.ID + " = @0", ID).FirstOrDefault(); dbEntity.SetPropertyValue("Status", type); var status = unitOfWork.SaveChanges(Guid.Empty); if (status == DataErrorCode.Locked) { return(NotificationType.Locked.ToString()); } else { return(NotificationType.Success.ToString()); } } catch (Exception ex) { return(NotificationType.Error + "," + ex.Message); } } }
///// <summary> ///// [Chuc.Nguyen] - Lưu một danh sách đối tượng ///// </summary> ///// <typeparam name="TEntity"></typeparam> ///// <param name="listEntity"></param> ///// <returns></returns> //public string Add<TEntity>(List<TEntity> listEntity) where TEntity : class //{ // using (var context = new VnrHrmDataContext()) // { // var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); // var repo = new CustomBaseRepository<TEntity>(unitOfWork); // try // { // repo.Add(listEntity); // repo.SaveChanges(); // return NotificationType.Success.ToString(); // } // catch (System.Data.Entity.Validation.DbEntityValidationException e) // { // throw; // } // catch (Exception ex) // { // return NotificationType.Error + "," + ex.Message; // } // } //} /// <summary> /// [Chuc.Nguyen] - Tạo mới một đối tượng /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <returns></returns> public string Add <TEntity>(List <TEntity> listEntity) where TEntity : class { using (var context = new VnrHrmDataContext()) { try { var status = DataErrorCode.Success; if (listEntity != null && listEntity.Count > 0) { var name = listEntity[0].GetType().Name.ToString(); Type dbEntityType = LibraryService.GetEntityType(name.Substring(0, name.Length - 6)); var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var listDbEntity = new List <object>(); foreach (var item in listEntity) { object dbEntity = Activator.CreateInstance(dbEntityType); item.CopyData(dbEntity); dbEntity.SetPropertyValue(Constant.DateCreate, DateTime.Now); listDbEntity.Add(dbEntity); } unitOfWork.AddObject(dbEntityType, listDbEntity.ToArray()); status = unitOfWork.SaveChanges(Guid.Empty); } if (status == DataErrorCode.Locked) { return(NotificationType.Locked.ToString()); } else { return(NotificationType.Success.ToString()); } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. return(NotificationType.Error + "," + exceptionMessage); } catch (Exception ex) { return(NotificationType.Error + "," + ex.Message); } } }
/// <summary> /// [Chuc.Nguyen] - Tạo mới một đối tượng /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="entity"></param> /// <returns></returns> public string Add <TEntity>(TEntity entity, Guid userID) where TEntity : class { using (var context = new VnrHrmDataContext()) { try { var name = entity.GetType().Name.ToString(); Type dbEntityType = LibraryService.GetEntityType(name.Substring(0, name.Length - 6)); var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); object dbEntity = Activator.CreateInstance(dbEntityType); entity.CopyData(dbEntity); Guid newID = Guid.NewGuid(); dbEntity.SetPropertyValue(Constant.ID, newID); dbEntity.SetPropertyValue(Constant.DateCreate, DateTime.Now); unitOfWork.AddObject(dbEntityType, dbEntity); if (dbEntityType == typeof(Hre_Profile)) { unitOfWork.GenerateCode(userID, "CodeEmp", dbEntity); } var status = unitOfWork.SaveChanges(userID); if (status == DataErrorCode.Locked) { return(NotificationType.Locked.ToString()); } else { entity.SetPropertyValue(Constant.ID, newID); return(NotificationType.Success.ToString()); } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. return(NotificationType.Error + "," + exceptionMessage); } catch (Exception ex) { return(NotificationType.Error + "," + ex.Message); } } }
public string Edit <TEntity>(TEntity entity, Guid userID) where TEntity : class { using (var context = new VnrHrmDataContext()) { try { var name = entity.GetType().Name.ToString(); //Type dbEntityType = LibraryService.GetEntityType(name.Substring(0, name.Length - 6)); Type dbEntityType = LibraryService.GetEntityType(name.Substring(0, name.LastIndexOf("Entity"))); var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var objectID = entity.GetPropertyValue(Constant.ID).TryGetValue <Guid>(); object dbEntity = unitOfWork.CreateQueryable(userID, dbEntityType, Constant.ID + " = @0", objectID).FirstOrDefault(); if (objectID != Guid.Empty) { entity.SetPropertyValue(Constant.DateCreate, dbEntity.GetPropertyValue(Constant.DateCreate)); entity.SetPropertyValue(Constant.UserCreate, dbEntity.GetPropertyValue(Constant.UserCreate)); entity.SetPropertyValue(Constant.IPCreate, dbEntity.GetPropertyValue(Constant.IPCreate)); entity.SetPropertyValue(Constant.ServerCreate, dbEntity.GetPropertyValue(Constant.ServerCreate)); } entity.CopyData(dbEntity, Constant.ID); var status = unitOfWork.SaveChanges(userID); if (status == DataErrorCode.Locked) { return(NotificationType.Locked.ToString()); } else { return(NotificationType.Success.ToString()); } } catch (System.Data.Entity.Validation.DbEntityValidationException e) { throw; } catch (Exception ex) { return(NotificationType.Error + "," + ex.Message); } } }