添加支持分表的删除方法

新增静态方法 `Delete(Expression? where)`,用于根据指定条件表达式从数据库中删除实体对象。该方法支持分表操作,增强了删除的灵活性和可扩展性。方法首先检查条件表达式是否为空,若为空则返回0;否则,根据分表策略决定调用现有的参数化删除方法或遍历分表进行删除。
This commit is contained in:
猿人易 2025-06-18 17:33:45 +08:00
parent db26ff2ab5
commit 31f864efeb
1 changed files with 36 additions and 0 deletions

View File

@ -1679,6 +1679,42 @@ public partial class Entity<TEntity> : EntityBase, IAccessor where TEntity : Ent
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Int32 Delete(String[] names, Object[] values) => Persistence.Delete(Meta.Session, names, values);
/// <summary>从数据库中删除指定条件表达式的实体对象,支持分表。</summary>
/// <param name="where">条件表达式</param>
/// <returns></returns>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Int32 Delete(Expression? where)
{
if (where == null || where.IsEmpty) return 0;
var session = Meta.Session;
// 自动分表
var shards = Meta.InShard || where == null ? null : Meta.ShardPolicy?.Shards(where);
if (shards == null || shards.Length == 0)
{
// 非分表情况直接调用现有的参数化Delete方法
var db = session.Dal.Db;
var ps = db.UseParameter ? new Dictionary<String, Object>() : null;
var whereClause = where?.GetString(db, ps) ?? String.Empty;
return Persistence.Delete(session, whereClause);
}
else
{
var count = 0;
foreach (var shard in shards)
{
var connName = shard.ConnName ?? session.ConnName;
var tableName = shard.TableName ?? session.TableName;
var shardSession = EntitySession<TEntity>.Create(connName, tableName);
count += Persistence.Delete(shardSession, where?.ToString() ?? String.Empty);
}
return count;
}
}
#endregion
#region SQL语句