升级到7.x.1.1
This commit is contained in:
parent
422e78c0d1
commit
2eb0d86f4c
|
@ -1,10 +1,10 @@
|
|||
:start
|
||||
::定义版本
|
||||
set EFCORE7=7.7.1.1-preview1
|
||||
set EFCORE6=7.6.1.1-preview1
|
||||
set EFCORE5=7.5.1.1-preview1
|
||||
set EFCORE3=7.3.1.1-preview1
|
||||
set EFCORE2=7.2.1.1-preview1
|
||||
set EFCORE7=7.7.1.1
|
||||
set EFCORE6=7.6.1.1
|
||||
set EFCORE5=7.5.1.1
|
||||
set EFCORE3=7.3.1.1
|
||||
set EFCORE2=7.2.1.1
|
||||
|
||||
::删除所有bin与obj下的文件
|
||||
@echo off
|
||||
|
|
|
@ -4,6 +4,9 @@ using ShardingCore.Sharding.Abstractions;
|
|||
using System;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
using ShardingCore.EFCores;
|
||||
using ShardingCore.Sharding.ShardingDbContextExecutors;
|
||||
using Volo.Abp.Domain.Entities;
|
||||
using Volo.Abp.EntityFrameworkCore;
|
||||
using Volo.Abp.Reflection;
|
||||
|
@ -14,6 +17,7 @@ namespace Samples.AbpSharding
|
|||
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext
|
||||
where TDbContext : DbContext
|
||||
{
|
||||
private bool _createExecutor = false;
|
||||
protected AbstractShardingAbpDbContext(DbContextOptions<TDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
@ -22,27 +26,41 @@ namespace Samples.AbpSharding
|
|||
private IShardingDbContextExecutor _shardingDbContextExecutor;
|
||||
public IShardingDbContextExecutor GetShardingExecutor()
|
||||
{
|
||||
return _shardingDbContextExecutor??=DoCreateShardingDbContextExecutor();
|
||||
if (!_createExecutor)
|
||||
{
|
||||
_shardingDbContextExecutor=this.DoCreateShardingDbContextExecutor();
|
||||
_createExecutor = true;
|
||||
}
|
||||
return _shardingDbContextExecutor;
|
||||
}
|
||||
|
||||
private IShardingDbContextExecutor DoCreateShardingDbContextExecutor()
|
||||
{
|
||||
var shardingDbContextExecutor = this.CreateShardingDbContextExecutor()!;
|
||||
|
||||
shardingDbContextExecutor.EntityCreateDbContextBefore += (sender, args) =>
|
||||
var shardingDbContextExecutor = this.CreateShardingDbContextExecutor();
|
||||
if (shardingDbContextExecutor != null)
|
||||
{
|
||||
CheckAndSetShardingKeyThatSupportAutoCreate(args.Entity);
|
||||
};
|
||||
shardingDbContextExecutor.CreateDbContextAfter += (sender, args) =>
|
||||
{
|
||||
var argsDbContext = args.DbContext;
|
||||
|
||||
if (argsDbContext is AbpDbContext<TDbContext> abpDbContext&&
|
||||
abpDbContext.LazyServiceProvider == null)
|
||||
|
||||
shardingDbContextExecutor.EntityCreateDbContextBefore += (sender, args) =>
|
||||
{
|
||||
abpDbContext.LazyServiceProvider = this.LazyServiceProvider;
|
||||
}
|
||||
};
|
||||
CheckAndSetShardingKeyThatSupportAutoCreate(args.Entity);
|
||||
};
|
||||
shardingDbContextExecutor.CreateDbContextAfter += (sender, args) =>
|
||||
{
|
||||
var dbContext = args.DbContext;
|
||||
if (dbContext is AbpDbContext<TDbContext> abpDbContext && abpDbContext.LazyServiceProvider == null)
|
||||
{
|
||||
abpDbContext.LazyServiceProvider = this.LazyServiceProvider;
|
||||
if (dbContext is IAbpEfCoreDbContext abpEfCoreDbContext)
|
||||
{
|
||||
abpEfCoreDbContext.Initialize(
|
||||
new AbpEfCoreDbContextInitializationContext(
|
||||
this.UnitOfWorkManager.Current
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
return shardingDbContextExecutor;
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ using Abp.Domain.Entities;
|
|||
using Abp.EntityFrameworkCore;
|
||||
using Abp.MultiTenancy;
|
||||
using Abp.Zero.EntityFrameworkCore;
|
||||
using JetBrains.Annotations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
|
@ -43,25 +44,34 @@ namespace Samples.AbpSharding
|
|||
|
||||
#region Sharding Core 方法实现
|
||||
|
||||
private bool _createExecutor = false;
|
||||
|
||||
private IShardingDbContextExecutor _shardingDbContextExecutor;
|
||||
public IShardingDbContextExecutor GetShardingExecutor()
|
||||
{
|
||||
return _shardingDbContextExecutor??=DoCreateShardingDbContextExecutor();
|
||||
if (!_createExecutor)
|
||||
{
|
||||
_shardingDbContextExecutor=this.DoCreateShardingDbContextExecutor();
|
||||
_createExecutor = true;
|
||||
}
|
||||
return _shardingDbContextExecutor;
|
||||
}
|
||||
private IShardingDbContextExecutor DoCreateShardingDbContextExecutor()
|
||||
{
|
||||
var shardingDbContextExecutor = this.CreateShardingDbContextExecutor()!;
|
||||
var shardingDbContextExecutor = this.CreateShardingDbContextExecutor();
|
||||
if (shardingDbContextExecutor != null)
|
||||
{
|
||||
shardingDbContextExecutor.EntityCreateDbContextBefore += (sender, args) =>
|
||||
{
|
||||
CheckAndSetShardingKeyThatSupportAutoCreate(args.Entity);
|
||||
};
|
||||
shardingDbContextExecutor.CreateDbContextAfter += (sender, args) =>
|
||||
{
|
||||
var argsDbContext = args.DbContext;
|
||||
FillDbContextInject(argsDbContext);
|
||||
};
|
||||
}
|
||||
|
||||
shardingDbContextExecutor.EntityCreateDbContextBefore += (sender, args) =>
|
||||
{
|
||||
CheckAndSetShardingKeyThatSupportAutoCreate(args.Entity);
|
||||
};
|
||||
shardingDbContextExecutor.CreateDbContextAfter += (sender, args) =>
|
||||
{
|
||||
var argsDbContext = args.DbContext;
|
||||
FillDbContextInject(argsDbContext);
|
||||
};
|
||||
return shardingDbContextExecutor;
|
||||
}
|
||||
|
||||
|
|
|
@ -146,7 +146,7 @@ namespace ShardingCore.Extensions
|
|||
var shardingRuntimeContext = dbContext.GetShardingRuntimeContext();
|
||||
var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
|
||||
|
||||
#if EFCORE6
|
||||
#if EFCORE6 || EFCORE7
|
||||
var entityTypes = contextModel.GetEntityTypes();
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
|
@ -157,7 +157,7 @@ namespace ShardingCore.Extensions
|
|||
}
|
||||
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
|
||||
var valueTuples =
|
||||
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType) ||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
|
||||
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsOnlyShardingDataSource(m.EntityType.ClrType)) ).Select(o => o.Key).ToList();
|
||||
for (int i = 0; i < valueTuples.Count; i++)
|
||||
{
|
||||
contextModelRelationalModel.Tables.Remove(valueTuples[i]);
|
||||
|
@ -174,7 +174,7 @@ namespace ShardingCore.Extensions
|
|||
}
|
||||
var contextModelRelationalModel = contextModel.RelationalModel as RelationalModel;
|
||||
var valueTuples =
|
||||
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsShardingDataSource(m.EntityType.ClrType)||entityMetadataManager.TryGet(m.EntityType.ClrType)==null)).Select(o => o.Key).ToList();
|
||||
contextModelRelationalModel.Tables.Where(o => o.Value.EntityTypeMappings.Any(m => !entityMetadataManager.IsOnlyShardingDataSource(m.EntityType.ClrType))).Select(o => o.Key).ToList();
|
||||
for (int i = 0; i < valueTuples.Count; i++)
|
||||
{
|
||||
contextModelRelationalModel.Tables.Remove(valueTuples[i]);
|
||||
|
@ -191,7 +191,7 @@ namespace ShardingCore.Extensions
|
|||
_data.Clear();
|
||||
}
|
||||
}
|
||||
var list = entityTypes.Where(o => !entityMetadataManager.IsShardingDataSource(o.Value.ClrType) || entityMetadataManager.TryGet(o.Value.ClrType) == null).Select(o => o.Key).ToList();
|
||||
var list = entityTypes.Where(o => !entityMetadataManager.IsOnlyShardingDataSource(o.Value.ClrType)).Select(o => o.Key).ToList();
|
||||
for (int i = 0; i < list.Count; i++)
|
||||
{
|
||||
entityTypes.Remove(list[i]);
|
||||
|
@ -216,7 +216,7 @@ namespace ShardingCore.Extensions
|
|||
var contextModel = dbContext.Model as Model;
|
||||
#endif
|
||||
|
||||
#if EFCORE6
|
||||
#if EFCORE6 || EFCORE7
|
||||
var contextModelRelationalModel = contextModel.GetRelationalModel() as RelationalModel;
|
||||
contextModelRelationalModel.Tables.Clear();
|
||||
#endif
|
||||
|
|
|
@ -35,7 +35,7 @@ namespace ShardingCore.Sharding
|
|||
|
||||
}
|
||||
|
||||
private IShardingDbContextExecutor? _shardingDbContextExecutor;
|
||||
private IShardingDbContextExecutor _shardingDbContextExecutor;
|
||||
public IShardingDbContextExecutor GetShardingExecutor()
|
||||
{
|
||||
if (!_createExecutor)
|
||||
|
|
|
@ -13,6 +13,10 @@ namespace ShardingCore.Sharding.Abstractions
|
|||
*/
|
||||
public interface IShardingDbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取分片执行者
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IShardingDbContextExecutor GetShardingExecutor();
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue