diff --git a/nuget-publish.bat b/nuget-publish.bat index 6daa4eaa..699451e8 100644 --- a/nuget-publish.bat +++ b/nuget-publish.bat @@ -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 diff --git a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs index 10ee3455..55f6c544 100644 --- a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs +++ b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs @@ -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 : AbpDbContext, IShardingDbContext where TDbContext : DbContext { + private bool _createExecutor = false; protected AbstractShardingAbpDbContext(DbContextOptions 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 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 abpDbContext && abpDbContext.LazyServiceProvider == null) + { + abpDbContext.LazyServiceProvider = this.LazyServiceProvider; + if (dbContext is IAbpEfCoreDbContext abpEfCoreDbContext) + { + abpEfCoreDbContext.Initialize( + new AbpEfCoreDbContextInitializationContext( + this.UnitOfWorkManager.Current + ) + ); + } + } + }; + } return shardingDbContextExecutor; } diff --git a/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs b/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs index cb648d8f..a21e38b4 100644 --- a/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs +++ b/samples/Samples.AbpSharding/AbstractShardingAbpZeroDbContext.cs @@ -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; } diff --git a/src/ShardingCore/Extensions/DbContextExtensions/DbContextExtensionShardingCore.cs b/src/ShardingCore/Extensions/DbContextExtensions/DbContextExtensionShardingCore.cs index fb7d2c0b..92cc5a40 100644 --- a/src/ShardingCore/Extensions/DbContextExtensions/DbContextExtensionShardingCore.cs +++ b/src/ShardingCore/Extensions/DbContextExtensions/DbContextExtensionShardingCore.cs @@ -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 diff --git a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs index 5a3f6b4c..97a8facf 100644 --- a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs +++ b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs @@ -35,7 +35,7 @@ namespace ShardingCore.Sharding } - private IShardingDbContextExecutor? _shardingDbContextExecutor; + private IShardingDbContextExecutor _shardingDbContextExecutor; public IShardingDbContextExecutor GetShardingExecutor() { if (!_createExecutor) diff --git a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs index e86e2313..bee1eb8e 100644 --- a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs +++ b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs @@ -13,6 +13,10 @@ namespace ShardingCore.Sharding.Abstractions */ public interface IShardingDbContext { + /// + /// 获取分片执行者 + /// + /// IShardingDbContextExecutor GetShardingExecutor();