//public virtual T Delete(T entity) //{ // return _dbset.Remove(entity); //} public virtual void Delete(T entity) { ISoftDelete e = entity; e.DeletedDate = DateTime.Now; e.IsDeleted = true; e.DeletedBy = 2; }
public static void Delete(this ISoftDelete entity) { entity.IsDeleted = true; var deletionAuditedEntity = entity.As <IDeletionAudited>(); deletionAuditedEntity.DeletionTime = DateTime.Now; deletionAuditedEntity.DeleterUserId = null; }
// <summary> //Undeletes this entity by setting<see cref="ISoftDelete.IsDeleted"/> to false and // <see cref = "IDeletionAudited" /> properties to null. // </summary> public static void UnDelete(this ISoftDelete entity) { entity.IsDeleted = false; var deletionAuditedEntity = entity.As <IDeletionAudited>(); deletionAuditedEntity.DeletionTime = null; deletionAuditedEntity.DeleterUserId = null; }
/// <summary> /// 取消删除。将<see cref="ISoftDelete.IsDeleted"/>设为false,并且将<see cref="IDeletionAuditedObject"/>属性设置为空 /// </summary> /// <param name="entity">逻辑删除实体</param> public static void UnDelete(this ISoftDelete entity) { entity.IsDeleted = false; if (entity is IDeletionAuditedObject deletionAuditedEntity) { deletionAuditedEntity.DeletionTime = null; deletionAuditedEntity.DeleterId = null; } }
public static void UdDelete(this ISoftDelete entity) { entity.IsDeleted = 0; if (entity is IDeletionAudited) { var deletionAuditedEntity = entity.As <IDeletionAudited>(); deletionAuditedEntity.DateDeleted = null; deletionAuditedEntity.DeleterUserId = null; } }
/// <summary> /// Undeletes this entity by setting <see cref="ISoftDelete.IsDeleted"/> to false and /// <see cref="IDeletionAudited"/> properties to null. /// </summary> public static void UnDelete(this ISoftDelete entity) { entity.IsDeleted = false; //if (entity is IDeletionAudited) //{ // var deletionAuditedEntity = entity.As<IDeletionAudited>(); // deletionAuditedEntity.DeletionTime = null; // deletionAuditedEntity.DeleterUserId = null; //} }
/// <summary> ///通过将“isoftDelete.isDeleted”设置为false和 /// “ideletionaudited”属性为空。 /// </summary> public static void UnDelete <TPrimaryKey>(this ISoftDelete entity) { entity.IsDeleted = false; if (entity is IDeletionAudited <TPrimaryKey> ) { var deletionAuditedEntity = entity.As <IDeletionAudited <TPrimaryKey> >(); deletionAuditedEntity.DeletionTime = null; deletionAuditedEntity.DeleterUserId = default(TPrimaryKey); } }
public virtual void Delete(TEntity entity) { if (entity is ISoftDelete) { ISoftDelete softDelete = entity as ISoftDelete; softDelete.Delete(); } else { Dbset.Remove(entity); } }
/// <summary> /// Initializes a new instance of the <see cref="AggregateNodeLookupCollectionMapper<TCollectionDto, TContainerEntity, TAggregateNode>"/> class. /// </summary> /// <param name="dtoCollection"> /// The dto collection. /// </param> /// <param name="containerEntity"> /// The container entity. /// </param> /// <param name="aggregateNodeCollection"> /// The aggregate node collection. /// </param> public AggregateNodeLookupCollectionMapper( ISoftDelete <TCollectionDto> dtoCollection, TContainerEntity containerEntity, IEnumerable <TAggregateNode> aggregateNodeCollection) { Check.IsNotNull(dtoCollection, "dtoCollection is required"); Check.IsNotNull(containerEntity, "containerEntity is required"); Check.IsNotNull(aggregateNodeCollection, "aggregateNodeCollection is required"); _dtoCollection = dtoCollection; _containerEntity = containerEntity; _aggregateNodeCollection = aggregateNodeCollection; }
public void Delete <T>(T entity) where T : class { if (entity is ISoftDelete) { ISoftDelete e = (ISoftDelete)entity; e.DeletedDate = DateTime.Now; e.IsDeleted = true; } else { _context.Remove(entity); } }
private void HandleSoftDeletableEntities() { string currentUser = Thread.CurrentPrincipal?.Identity?.Name; var entries = ChangeTracker.Entries().Where(x => x.Entity is ISoftDelete && x.State == EntityState.Deleted); foreach (var entry in entries) { entry.State = EntityState.Modified; ISoftDelete entity = (ISoftDelete)entry.Entity; entity.IsDeleted = true; entity.DeleterUser = currentUser; entity.DeletionTime = DateTime.Now; } }
/// <summary> /// 检查删除 /// </summary> /// <param name="entity">实体</param> /// <returns></returns> private void CheckDelete(TEntity entity) { if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) { ISoftDelete softDeletabl = (ISoftDelete)entity; softDeletabl.IsDeleted = true; var entity1 = (TEntity)softDeletabl; this._dbContext.Update(entity1); } else { this._dbContext.Remove(entity); } }
public virtual void Delete(long id) { if (typeof(TEntity).IsAssignableFrom(typeof(ISoftDelete))) { var entity = Get(id); ISoftDelete softDelete = entity as ISoftDelete; softDelete.Delete(); } else { var entity = new TEntity(); entity.SetId(id); Dbset.Remove(entity); } }
private void DeleteInternal(params TEntity[] entities) { CheckDataAuth(DataAuthOperation.Delete, entities); if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) { // 逻辑删除 foreach (TEntity entity in entities) { ISoftDelete softDeletableEntity = (ISoftDelete)entity; softDeletableEntity.IsDeleted = true; } } else { // 物理删除 _dbSet.RemoveRange(entities); } }
/// <summary> /// 异步删除所有符合特定条件的实体 /// </summary> /// <param name="predicate">查询条件谓语表达式</param> /// <returns>操作影响的行数</returns> public virtual async Task <int> DeleteBatchAsync(Expression <Func <TEntity, bool> > predicate) { Check.NotNull(predicate, nameof(predicate)); await((DbContextBase)_dbContext).BeginOrUseTransactionAsync(_cancellationTokenProvider.Token); // TODO: 檢查性能 逻辑删除 TEntity[] entities = _dbSet.Where(predicate).ToArray(); // TODO: 检测删除的数据权限 CheckDataAuth(DataAuthOperation.Delete, entities); //TEntity[] entities = _dbSet.Where(predicate).AsTracking().ToArray(); //// 检测删除的数据权限 //CheckDataAuth(DataAuthOperation.Delete, entities); if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity))) { foreach (TEntity entity in entities) { ISoftDelete softDeletableEntity = (ISoftDelete)entity; softDeletableEntity.IsDeleted = true; } return(await _dbContext.SaveChangesAsync(_cancellationTokenProvider.Token)); //// TODO: nameof(ISoftDelete) //// Create a MemberBinding object for each member //// that you want to initialize. //MemberBinding speciesMemberBinding = // Expression.Bind( // typeof(TEntity).GetMember("IsDeleted")[0], // Expression.Constant(true)); //// Create a MemberInitExpression that represents initializing //MemberInitExpression memberInitExpression = // Expression.MemberInit( // Expression.New(typeof(TEntity)), // speciesMemberBinding); //ParameterExpression input = Expression.Parameter(typeof(TEntity), "p"); //Expression<Func<TEntity, TEntity>> updateExpression = Expression.Lambda<Func<TEntity, TEntity>>(memberInitExpression, input); //return await _dbSet.Where(predicate).UpdateAsync(updateExpression); } // 物理删除 return(await _dbSet.Where(predicate).DeleteAsync(_cancellationTokenProvider.Token)); }
private void AddListener(ISoftDelete softDelete, object pObj) { var softDeletedInput = ( from evt in Observable.FromEventPattern <SoftDeletedEventArgs> (softDelete, "SoftDeleted") select(evt) ); var softDeletedSubscriber = softDeletedInput.ObserveOnDispatcher().Subscribe( (evt) => { SoftDeleteSoftDeleted(evt.Sender, evt.EventArgs); var pEditingDto = pObj as EditableDataTransferObject; if (pEditingDto != null && pEditingDto.Key > 0) { pEditingDto.EditStatus = EditStatus.Update; } } ); _subscriptions.Add(softDeletedSubscriber); }
protected override void DeleteEntity(IEventSource session, object entity, EntityEntry entityEntry, bool isCascadeDeleteEnabled, IEntityPersister persister, ISet transientEntities) { if (!(entity is ISoftDelete)) { base.DeleteEntity(session, entity, entityEntry, isCascadeDeleteEnabled, persister, transientEntities); return; } ISoftDelete softDeleteEntity = (ISoftDelete)entity; softDeleteEntity.Deleted = true; softDeleteEntity.DeleteDate = DateTime.Now; if (log.IsDebugEnabled) { log.Debug("temporal deleting " + MessageHelper.InfoString(persister, entityEntry.Id, session.Factory)); } CascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities); CascadeAfterDelete(session, persister, entity, transientEntities); }
/// <summary> /// Check if this Entity is null of marked as deleted. /// </summary> public static bool IsNullOrDeleted(this ISoftDelete entity) { return(entity == null || entity.IsDeleted); }
/// <summary> /// Initializes a new instance of the <see cref="AggregateRootCollectionMapper<TCollectionDto, TAggregateRoot>"/> class. /// </summary> /// <param name="dtoCollection"> /// The dto collection. /// </param> public AggregateRootCollectionMapper(ISoftDelete <TCollectionDto> dtoCollection) { Check.IsNotNull(dtoCollection, "dtoCollection is required"); _dtoCollection = dtoCollection; }
/// <summary> /// 删除实体 /// </summary> /// <param name="entity"></param> public static void Delete(this ISoftDelete entity) { entity.IsDeleted = true; }
/// <summary> ///取消删除实体 /// </summary> public static void UnDelete(this ISoftDelete entity) { entity.IsDeleted = false; }
private static void DeleteItem(ISoftDelete deletable) { //deletable.DeleteAssociation(); //deletable.Item = deletable.Item; deletable.Delete(); }
private void AddListener( ISoftDelete softDelete, object pObj ) { var softDeletedInput = ( from evt in Observable.FromEventPattern<SoftDeletedEventArgs> ( softDelete, "SoftDeleted" ) select ( evt ) ); var softDeletedSubscriber = softDeletedInput.ObserveOnDispatcher ().Subscribe ( ( evt ) => { SoftDeleteSoftDeleted ( evt.Sender, evt.EventArgs ); var pEditingDto = pObj as EditableDataTransferObject; if ( pEditingDto != null && pEditingDto.Key > 0 ) { pEditingDto.EditStatus = EditStatus.Update; } } ); _subscriptions.Add ( softDeletedSubscriber ); }
public static void SoftDelete(this ISoftDelete model) { model.SoftDelete = DateTime.UtcNow; }
private void SoftDelete(ISoftDelete entity) { entity.IsDeleted = true; entity.Deleted = DateTime.UtcNow; }
public override int SaveChanges() { List <AuditReport> tAuditReport = new List <AuditReport>(); foreach (var tEntry in ChangeTracker.Entries()) { if ((tEntry.State == EntityState.Added || tEntry.State == EntityState.Modified || tEntry.State == EntityState.Deleted) && tEntry.Entity is IAuditedEntity) { string entityName = tEntry.Entity.GetType().Name; string entityType = tEntry.State.ToString(); var tempKeyEntity = tEntry.Entity.GetType().GetProperties().Where(x => x.CustomAttributes.Where(y => y.AttributeType.Name == "KeyAttribute").Count() > 0).FirstOrDefault(); string primaryKey = "0"; if (tempKeyEntity != null && tEntry.State != EntityState.Added) { primaryKey = tempKeyEntity.GetValue(tEntry.Entity).ToString(); } if (tEntry.State == EntityState.Deleted) { AuditReport tReport = new AuditReport() { PrimaryKey = primaryKey, EntityName = entityName, Type = entityType }; tAuditReport.Add(tReport); } else { foreach (var prop in tEntry.Properties) { if (tEntry.State == EntityState.Modified && prop.CurrentValue.ToString() == prop.OriginalValue.ToString()) { continue; } AuditReport tReport = new AuditReport() { PrimaryKey = primaryKey, EntityName = entityName, Type = entityType, NewValue = prop.CurrentValue.ToString(), Column = prop.Metadata.Name }; if (tEntry.State == EntityState.Modified) { tReport.OldValue = prop.OriginalValue.ToString(); } tAuditReport.Add(tReport); } } } if (tEntry.State == EntityState.Added) { if (tEntry.Entity is IAuditedEntity) { IAuditedEntity tempEntity = (IAuditedEntity)tEntry.Entity; tempEntity.AddedBy = 1; tempEntity.AddedDate = DateTime.Now; } } else if (tEntry.State == EntityState.Modified) { if (tEntry.Entity is IAuditedEntity) { IAuditedEntity tempEntity = (IAuditedEntity)tEntry.Entity; tempEntity.UpdatedBy = 1; tempEntity.UpdateDate = DateTime.Now; } } else if (tEntry.State == EntityState.Deleted) { tEntry.Reload(); tEntry.State = EntityState.Modified; if (tEntry.Entity is ISoftDelete) { IAuditedEntity tempEntity = (IAuditedEntity)tEntry.Entity; tempEntity.DeletedBy = 1; tempEntity.DeletedDate = DateTime.Now; ISoftDelete softDeleteEntity = (ISoftDelete)tEntry.Entity; tempEntity.IsDeleted = true; } } } AuditReport.AddRange(tAuditReport); return(base.SaveChanges()); }
/// <summary> /// Check if this Entity is null of marked as deleted. /// </summary> public static bool IsNullOrDeleted(this ISoftDelete entity) => entity == null || entity.IsDeleted;