Exemplo n.º 1
0
        public int Delete(TEntity entity, bool logicalDelete = true)
        {
            EntityPersistentSubscribePublisher.OnBeforeRemove(entity);

            int result;

            if ((result = Queryable.RemoveEntity(entity, logicalDelete)) > 0)
            {
                EntityPersistentSubscribePublisher.OnAfterRemove(entity);
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 将指定的实体对象从库中移除。
        /// </summary>
        /// <param name="entity">要移除的实体对象。</param>
        /// <param name="fakeProperty">标识假删除的属性。</param>
        /// <param name="fake">如果具有 IsDeletedKey 属性,则提供对数据假删除的支持。</param>
        /// <exception cref="NullReferenceException">对象内部的 Database 为 null。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="entity"/> 参数为 null。</exception>
        public void Remove(IEntity entity, IProperty fakeProperty, bool fake = true)
        {
            EntityPersistentSubscribePublisher.OnBeforeRemove(entity);
            try
            {
                Batch(() =>
                {
                    var parameters = new ParameterCollection();
                    var context    = CreateContext(parameters);

                    var canFake = fakeProperty != null && fake;
                    var sql     = canFake ? EntityPersistentQueryBuilder.BuildUpdateFakeDeleteQuery(context, entity, fakeProperty)
                            : EntityPersistentQueryBuilder.BuildDeleteQuery(context, entity);

                    var isSucc = false;
                    entity.TryLockModifing(() =>
                    {
                        if (!sql.IsNullOrEmpty())
                        {
                            isSucc = database.ExecuteNonQuery(sql, parameters) > 0;
                        }

                        UpdateRelations(entity);
                    });
                    if (isSucc)
                    {
                        EntityPersistentSubscribePublisher.OnAfterRemove(entity);
                        entity.SetState(EntityState.Unchanged);
                    }
                });
            }
            catch (DbException exp)
            {
                throw new EntityPersistentException(SR.GetString(SRKind.FailInEntityRemove), exp);
            }
        }