修复批处理解析时创建的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 :start
::定义版本 ::定义版本
set EFCORE2=2.3.1.22 set EFCORE2=2.3.1.23
set EFCORE3=3.3.1.22 set EFCORE3=3.3.1.23
set EFCORE5=5.3.1.22 set EFCORE5=5.3.1.23
::删除所有bin与obj下的文件 ::删除所有bin与obj下的文件
@echo off @echo off

View File

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

View File

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

View File

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

View File

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

View File

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