/// <summary> /// 将实体对象从数据库中删除,调用该方法时实体对象必须具备主键属性 /// </summary> /// <param name="object">实体对象</param> public virtual int Delete(object @object) { Type elementType = @object.GetType(); if (@object.GetType().IsAssignableFrom(typeof(IEntityObject))) { elementType = @object.GetType().BaseType; } IDeleteable deleteable = this.factory.CreateDeleteProvider(this).CreateDelete(elementType); var properties = elementType.GetProperties(BindingFlags.Public | BindingFlags.Instance); properties.Where(property => property.IsPrimaryKey()).Each(property => { ParameterExpression pe = Expression.Parameter(@object.GetType(), "s"); MemberExpression me = Expression.MakeMemberAccess(pe, property); ConstantExpression ce = Expression.Constant(property.EmitGetValue(@object), property.PropertyType); BinaryExpression be = Expression.Equal(me, ce); deleteable = deleteable.Where(be); }); return(deleteable.Execute(elementType)); }
protected virtual void ApplyDeleteFilterList(IDeleteable <TEntity> deleteable) { if (_filters[GetFilterIndex(nameof(ISoftDelete))].IsEnabled && GetInterfacesOfTEntity().Contains(typeof(ISoftDelete))) { var dbColumnName = Context.EntityMaintenance.GetDbColumnName <TEntity>(nameof(ISoftDelete.IsDeleted)); deleteable = deleteable.Where($"{dbColumnName} = 0"); } }
/// <summary> /// 按条件删除(物理删除) /// </summary> /// <typeparam name="T">表实体</typeparam> /// <param name="where">条件语句,如:"id=@id"</param> /// <param name="parameters">参数,如:new{id=0}</param> /// <returns>int</returns> public int Delete <T>(string where, object parameters) where T : class, new() { IDeleteable <T> r = _db.Deleteable <T>(); if (!string.IsNullOrEmpty(where)) { r = r.Where(where, parameters); } return(r.ExecuteCommand()); }