Skip to content

Commit

Permalink
添加zzz的bulk的封装示例
Browse files Browse the repository at this point in the history
  • Loading branch information
xuejmnet committed Nov 11, 2022
1 parent 0ea3b47 commit 1d15a8d
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions benchmarks/ShardingCoreBenchmark/ZZZDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
using ShardingCore6x.ShardingDbContexts;
using Z.EntityFramework.Plus;

namespace ShardingCore6x
{
public class ZZZDemo
{
private readonly List<Order> _orders = new List<Order>();
private readonly DefaultShardingDbContext _dbcontext;

public async Task BulkInsertAsync()
{
// dbcontext.BulkShardingEnumerable(orders);//如果有分库用这个
var orderWithShardingGroup = _dbcontext.BulkShardingTableEnumerable(_orders);
using (var tran = await _dbcontext.Database.BeginTransactionAsync())
{
foreach (var orderWithDbContext in orderWithShardingGroup)
{
await orderWithDbContext.Key.BulkInsertAsync(orderWithDbContext.Value);
}

await tran.CommitAsync();
}
}

public async Task BulkUpdateAsync()
{
// dbcontext.BulkShardingExpression(orders);//如果有分库用这个
Expression<Func<Order, bool>> where = o => o.Id == "123";
var dbContexts = _dbcontext.BulkShardingTableExpression(where);
using (var tran = await _dbcontext.Database.BeginTransactionAsync())
{
foreach (var dbContext in dbContexts)
{
await dbContext.Set<Order>().Where(where).UpdateAsync(o => new Order()
{
Body = o.Body + "123"
});
}

await tran.CommitAsync();
}
}
public async Task BulkUpdateAsync2()
{
await _dbcontext.ShardingBulkUpdateAsync<Order>(o => o.Id == "123", o => new Order()
{
Body = o.Body + "123"
});
}
}

internal static class ZZZExtension
{
public static async Task<int> ShardingBulkUpdateAsync<T>(this DefaultShardingDbContext shardingDbContext, Expression<Func<T, bool>> where,
Expression<Func<T, T>> updateFactory,
CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
var dbContexts = shardingDbContext.BulkShardingTableExpression(where);
var effectRows = 0;
foreach (var dbContext in dbContexts)
{
effectRows += await dbContext.Set<T>().Where(where)
.UpdateAsync(updateFactory, cancellationToken: cancellationToken);
}

return effectRows;
}
}
}

0 comments on commit 1d15a8d

Please sign in to comment.