修复批处理解析时创建的dbcontext不会加入到当前的事务的bug

This commit is contained in:
xuejiaming 2021-11-05 21:03:54 +08:00
parent 835db61507
commit a680f07a92
6 changed files with 54 additions and 51 deletions

View File

@ -1,8 +1,8 @@
:start
::定义版本
set EFCORE2=2.3.1.22
set EFCORE3=3.3.1.22
set EFCORE5=5.3.1.22
set EFCORE2=2.3.1.23
set EFCORE3=3.3.1.23
set EFCORE5=5.3.1.23
::删除所有bin与obj下的文件
@echo off

View File

@ -4,6 +4,7 @@ using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
using EFCore.BulkExtensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
@ -122,47 +123,50 @@ namespace Sample.SqlServer.Controllers
shardingPageResultAsync
});
}
//[HttpGet]
//public IActionResult Get3()
//{
[HttpGet]
public IActionResult Get3()
{
// var dbContext2s = _defaultTableDbContext.BulkShardingExpression<SysUserMod>(o => o.Age > 100);
// using (var tran = _defaultTableDbContext.Database.BeginTransaction())
// {
// dbContext2s.ForEach(dbContext =>
// {
// dbContext.Set<SysUserMod>().Where(o => o.Age > 100).Update(o => new SysUserMod()
// {
// AgeGroup = 1000
// });
// });
// _defaultTableDbContext.SaveChanges();
// tran.Commit();
// }
// var list = new List<SysUserMod>();
// var dbContexts = _defaultTableDbContext.BulkShardingEnumerable(list);
//var dbContext2s = _defaultTableDbContext.BulkShardingExpression<SysUserMod>(o => o.Age > 100);
//using (var tran = _defaultTableDbContext.Database.BeginTransaction())
//{
// dbContext2s.ForEach(dbContext =>
// {
// dbContext.Set<SysUserMod>().Where(o => o.Age > 100).Update(o => new SysUserMod()
// {
// AgeGroup = 1000
// });
// });
// _defaultTableDbContext.SaveChanges();
// tran.Commit();
//}
var list = new List<SysUserMod>();
for (int i = 0; i < 100; i++)
{
list.Add(new SysUserMod()
{
Id =i.ToString(),
Name = i.ToString(),
Age = i,
AgeGroup = i
});
}
// using (var tran = _defaultTableDbContext.Database.BeginTransaction())
// {
// dbContexts.ForEach(kv =>
// {
// kv.Key.BulkInsert(kv.Value);
// });
// dbContexts.ForEach(kv =>
// {
// kv.Key.BulkDelete(kv.Value);
// });
// dbContexts.ForEach(kv =>
// {
// kv.Key.BulkUpdate(kv.Value);
// });
// _defaultTableDbContext.SaveChanges();
// tran.Commit();
// }
using (var tran = _defaultTableDbContext.Database.BeginTransaction())
{
var dbContexts = _defaultTableDbContext.BulkShardingTableEnumerable(list);
dbContexts.ForEach(kv =>
{
kv.Key.BulkInsert(kv.Value.ToList());
});
var a = 0;
var b = 1 / a;
tran.Commit();
}
// return Ok();
//}
return Ok();
}
}
}

View File

@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EFCore.BulkExtensions" Version="5.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.10" />
</ItemGroup>

View File

@ -82,7 +82,7 @@ namespace Sample.SqlServer
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
app.DbSeed();
//app.DbSeed();
}
}
}

View File

@ -187,7 +187,7 @@ namespace ShardingCore.Extensions
var routeTailIdentity = routeTail.GetRouteTailIdentity();
if (!dataSourceBulkDicEntries.TryGetValue(routeTailIdentity, out var bulkDicEntry))
{
var dbContext = shardingDbContext.GetDbContext(dataSourceName, true, routeTail);
var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, routeTail);
bulkDicEntry = new BulkDicEntry<TEntity>(dbContext, new LinkedList<TEntity>());
dataSourceBulkDicEntries.Add(routeTailIdentity, bulkDicEntry);
}
@ -202,7 +202,7 @@ namespace ShardingCore.Extensions
var routeTailIdentity = routeTail.GetRouteTailIdentity();
if (!dataSourceBulkDicEntries.TryGetValue(routeTailIdentity, out var bulkDicEntry))
{
var dbContext = shardingDbContext.GetDbContext(dataSourceName, true, routeTail);
var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, routeTail);
bulkDicEntry = new BulkDicEntry<TEntity>(dbContext, new LinkedList<TEntity>());
dataSourceBulkDicEntries.Add(routeTailIdentity, bulkDicEntry);
}
@ -267,7 +267,7 @@ namespace ShardingCore.Extensions
if (physicTables.IsEmpty())
throw new ShardingCoreException($"{where.ShardingPrint()} cant found ant physic table");
var dbs = physicTables.Select(o => shardingDbContext.GetDbContext(dataSourceName, true, routeTailFactory.Create(o.Tail))).ToList();
var dbs = physicTables.Select(o => shardingDbContext.GetDbContext(dataSourceName, false, routeTailFactory.Create(o.Tail))).ToList();
foreach (var dbContext in dbs)
{
dbContexts.AddLast(dbContext);
@ -275,7 +275,7 @@ namespace ShardingCore.Extensions
}
else
{
var dbContext = shardingDbContext.GetDbContext(dataSourceName, true, routeTailFactory.Create(string.Empty));
var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, routeTailFactory.Create(string.Empty));
dbContexts.AddLast(dbContext);
}

View File

@ -133,9 +133,8 @@ namespace ShardingCore.Core.Internal.Visitors
private Expression<Func<string, bool>> Resolve(Expression expression)
{
if (expression is LambdaExpression)
if (expression is LambdaExpression lambda)
{
LambdaExpression lambda = expression as LambdaExpression;
expression = lambda.Body;
return Resolve(expression);
}
@ -145,13 +144,12 @@ namespace ShardingCore.Core.Internal.Visitors
return ParseGetWhere(binaryExpression);
}
if (expression is UnaryExpression) //解析一元运算符
if (expression is UnaryExpression unary) //解析一元运算符
{
UnaryExpression unary = expression as UnaryExpression;
if (unary.Operand is MethodCallExpression methodCall1Expression)
if (unary.Operand is MethodCallExpression unaryCallExpression)
{
// return ResolveLinqToObject(unary.Operand, false);
return ResolveInFunc(methodCall1Expression, unary.NodeType != ExpressionType.Not);
return ResolveInFunc(unaryCallExpression, unary.NodeType != ExpressionType.Not);
}
}