添加当前dbcontext是否是执行的dbcontext的属性

This commit is contained in:
xuejiaming 2023-01-11 16:43:23 +08:00
parent a402ccd674
commit 8a3ff2721d
4 changed files with 23 additions and 11 deletions

View File

@ -1,6 +1,8 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
namespace Sample.AutoCreateIfPresent.Controllers;

View File

@ -61,14 +61,13 @@ namespace Sample.MySql.Controllers
public IQueryable<SysTest> GetAll()
{
var shardingRouteManager = _shardingRuntimeContext.GetShardingRouteManager();
// var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
// var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
// //系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
// var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod));
// //一定要先在路由里面添加尾巴
// virtualTableRoute.Append("20220921");
// shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
var shardingTableCreator = _shardingRuntimeContext.GetShardingTableCreator();
var tableRouteManager = _shardingRuntimeContext.GetTableRouteManager();
//系统的时间分片都会实现 ITailAppendable 如果不是系统的自定义的转成你自己的对象即可
var virtualTableRoute = (ITailAppendable)tableRouteManager.GetRoute(typeof(SysUserMod));
//一定要先在路由里面添加尾巴
virtualTableRoute.Append("20220921");
shardingTableCreator.CreateTable<SysUserMod>("ds0","20220921");
return _defaultTableDbContext.Set<SysTest>();
}
[HttpGet]

View File

@ -20,7 +20,7 @@ namespace ShardingCore.Extensions
{
public static class ShardingDbContextExtension
{
public static IShardingDbContextExecutor? CreateShardingDbContextExecutor<TDbContext>(
public static IShardingDbContextExecutor CreateShardingDbContextExecutor<TDbContext>(
this TDbContext shellDbContext)
where TDbContext:DbContext,IShardingDbContext
{

View File

@ -25,6 +25,7 @@ namespace ShardingCore.Sharding
/// </summary>
public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext
{
private bool _createExecutor = false;
/// <summary>
/// 构造函数
/// </summary>
@ -34,12 +35,22 @@ namespace ShardingCore.Sharding
}
private IShardingDbContextExecutor _shardingDbContextExecutor;
private IShardingDbContextExecutor? _shardingDbContextExecutor;
public IShardingDbContextExecutor GetShardingExecutor()
{
return _shardingDbContextExecutor??=this.CreateShardingDbContextExecutor();
if (!_createExecutor)
{
_shardingDbContextExecutor=this.CreateShardingDbContextExecutor();
_createExecutor = true;
}
return _shardingDbContextExecutor;
}
/// <summary>
/// 当前dbcontext是否是执行的dbcontext
/// </summary>
public bool IsExecutor => GetShardingExecutor() == default;
public override void Dispose()
{
_shardingDbContextExecutor?.Dispose();