From 413b46978df3b8fede7350fbd720aade3fc20b7f Mon Sep 17 00:00:00 2001 From: xuejiaming <326308290@qq.com> Date: Mon, 11 Oct 2021 20:58:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9dbcontext=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E6=97=A0=E6=B3=9B=E5=9E=8B=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DbContexts/MyDbContext.cs | 35 ----- .../DbContexts/MyShardingDbContext.cs | 6 +- samples/Sample.BulkConsole/Program.cs | 2 +- .../DefaultDesignTimeDbContextFactory.cs | 2 +- .../EFCores/DefaultShardingTableDbContext.cs | 6 +- .../EFCores/DefaultTableDbContext.cs | 28 ---- samples/Sample.Migrations/Startup.cs | 2 +- samples/Sample.MySql/DIExtension.cs | 2 +- .../DbContexts/DefaultShardingDbContext.cs | 5 +- .../DbContexts/DefaultTableDbContext.cs | 26 ---- .../DbContexts/DefaultShardingDbContext.cs | 8 +- .../DbContexts/DefaultTableDbContext.cs | 25 ---- samples/Sample.SqlServer/Startup.cs | 2 +- .../DbContexts/DefaultDbContext.cs | 23 --- .../DbContexts/DefaultShardingDbContext.cs | 2 +- .../Startup.cs | 2 +- .../AbstractShardingAbpDbContext.cs | 21 +-- .../DbContexts/DefaultShardingDbContext.cs | 5 +- .../DbContexts/DefaultTableDbContext.cs | 24 --- .../Samples.AutoByDate.SqlServer/Startup.cs | 2 +- src/ShardingCore/DIExtension.cs | 18 +-- .../DIExtensions/ShardingCoreConfigBuilder.cs | 9 +- .../ShardingCoreConfigEndBuilder.cs | 14 +- .../ShardingDataBaseOrTableBuilder.cs | 17 +-- .../ShardingDataSourceRouteBuilder.cs | 13 +- .../ShardingDefaultDataSourceBuilder.cs | 13 +- .../DIExtensions/ShardingQueryBuilder.cs | 13 +- .../ShardingReadWriteSeparationBuilder.cs | 13 +- .../DIExtensions/ShardingTableBuilder.cs | 13 +- .../ShardingTransactionBuilder.cs | 13 +- .../DbContexts/ShardingDbContextFactory.cs | 9 ++ .../DefaultShardingDbContextCreatorConfig.cs | 9 +- .../EFCores/ShardingInternalDbSet.cs | 38 ++--- .../IShardingDbContextCreatorConfig.cs | 1 - .../Sharding/AbstractShardingDbContext.cs | 137 ++++++++++++++++-- .../Abstractions/IShardingDbContext.cs | 14 +- .../ShardingDbContextExecutor.cs | 12 +- test/ShardingCore.Test50/DefaultDbContext.cs | 32 ---- .../ShardingDefaultDbContext.cs | 5 +- test/ShardingCore.Test50/Startup.cs | 2 +- .../DefaultDbContext.cs | 31 ---- .../ShardingDefaultDbContext.cs | 5 +- test/ShardingCore.Test50_2x/Startup.cs | 2 +- .../DefaultDbContext.cs | 31 ---- .../ShardingDefaultDbContext.cs | 5 +- test/ShardingCore.Test50_3x/Startup.cs | 2 +- 46 files changed, 286 insertions(+), 413 deletions(-) delete mode 100644 samples/Sample.BulkConsole/DbContexts/MyDbContext.cs delete mode 100644 samples/Sample.Migrations/EFCores/DefaultTableDbContext.cs delete mode 100644 samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs delete mode 100644 samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs delete mode 100644 samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultDbContext.cs delete mode 100644 samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultTableDbContext.cs delete mode 100644 test/ShardingCore.Test50/DefaultDbContext.cs delete mode 100644 test/ShardingCore.Test50_2x/DefaultDbContext.cs delete mode 100644 test/ShardingCore.Test50_3x/DefaultDbContext.cs diff --git a/samples/Sample.BulkConsole/DbContexts/MyDbContext.cs b/samples/Sample.BulkConsole/DbContexts/MyDbContext.cs deleted file mode 100644 index 9adcf3ad..00000000 --- a/samples/Sample.BulkConsole/DbContexts/MyDbContext.cs +++ /dev/null @@ -1,35 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Sample.BulkConsole.Entities; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding.Abstractions; - -namespace Sample.BulkConsole.DbContexts -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/9/7 21:07:19 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - public class MyDbContext: DbContext,IShardingTableDbContext - { - public MyDbContext(DbContextOptions myDbContextOptions):base(myDbContextOptions) - { - - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.Entity(entity => - { - entity.HasKey(o => o.Id); - entity.Property(o => o.OrderNo).IsRequired().HasMaxLength(128).IsUnicode(false); - entity.ToTable(nameof(Order)); - }); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/samples/Sample.BulkConsole/DbContexts/MyShardingDbContext.cs b/samples/Sample.BulkConsole/DbContexts/MyShardingDbContext.cs index b68e6f44..187f6ca1 100644 --- a/samples/Sample.BulkConsole/DbContexts/MyShardingDbContext.cs +++ b/samples/Sample.BulkConsole/DbContexts/MyShardingDbContext.cs @@ -1,7 +1,9 @@ using System; using Microsoft.EntityFrameworkCore; using Sample.BulkConsole.Entities; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; namespace Sample.BulkConsole.DbContexts { @@ -12,7 +14,7 @@ namespace Sample.BulkConsole.DbContexts * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class MyShardingDbContext:AbstractShardingDbContext + public class MyShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext { public MyShardingDbContext(DbContextOptions options) : base(options) { @@ -29,5 +31,7 @@ namespace Sample.BulkConsole.DbContexts entity.ToTable(nameof(Order)); }); } + + public IRouteTail RouteTail { get; set; } } } diff --git a/samples/Sample.BulkConsole/Program.cs b/samples/Sample.BulkConsole/Program.cs index 5eb6e6d1..3cea7ff4 100644 --- a/samples/Sample.BulkConsole/Program.cs +++ b/samples/Sample.BulkConsole/Program.cs @@ -24,7 +24,7 @@ namespace Sample.BulkConsole { var services = new ServiceCollection(); services.AddLogging(); - services.AddShardingDbContext( + services.AddShardingDbContext( o => o.UseSqlServer("Data Source=localhost;Initial Catalog=MyOrderSharding;Integrated Security=True;")) .Begin(o => { diff --git a/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs b/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs index d7b2c787..924b3fec 100644 --- a/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs +++ b/samples/Sample.Migrations/DefaultDesignTimeDbContextFactory.cs @@ -16,7 +16,7 @@ namespace Sample.Migrations static DefaultDesignTimeDbContextFactory() { var services = new ServiceCollection(); - services.AddShardingDbContext( + services.AddShardingDbContext( o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBMigration;Integrated Security=True;") .ReplaceService>() diff --git a/samples/Sample.Migrations/EFCores/DefaultShardingTableDbContext.cs b/samples/Sample.Migrations/EFCores/DefaultShardingTableDbContext.cs index 29e15797..9ec55425 100644 --- a/samples/Sample.Migrations/EFCores/DefaultShardingTableDbContext.cs +++ b/samples/Sample.Migrations/EFCores/DefaultShardingTableDbContext.cs @@ -3,11 +3,13 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; namespace Sample.Migrations.EFCores { - public class DefaultShardingTableDbContext:AbstractShardingDbContext + public class DefaultShardingTableDbContext:AbstractShardingDbContext, IShardingTableDbContext { public DefaultShardingTableDbContext(DbContextOptions options) : base(options) { @@ -20,5 +22,7 @@ namespace Sample.Migrations.EFCores modelBuilder.ApplyConfiguration(new ShardingWithModMap()); modelBuilder.ApplyConfiguration(new ShardingWithDateTimeMap()); } + + public IRouteTail RouteTail { get; set; } } } diff --git a/samples/Sample.Migrations/EFCores/DefaultTableDbContext.cs b/samples/Sample.Migrations/EFCores/DefaultTableDbContext.cs deleted file mode 100644 index 01302166..00000000 --- a/samples/Sample.Migrations/EFCores/DefaultTableDbContext.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding.Abstractions; - -namespace Sample.Migrations.EFCores -{ - public class DefaultTableDbContext:DbContext,IShardingTableDbContext - { - public DefaultTableDbContext(DbContextOptions options):base(options) - { - - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new NoShardingTableMap()); - modelBuilder.ApplyConfiguration(new ShardingWithModMap()); - modelBuilder.ApplyConfiguration(new ShardingWithDateTimeMap()); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/samples/Sample.Migrations/Startup.cs b/samples/Sample.Migrations/Startup.cs index cf21535e..6dbbc8c7 100644 --- a/samples/Sample.Migrations/Startup.cs +++ b/samples/Sample.Migrations/Startup.cs @@ -31,7 +31,7 @@ namespace Sample.Migrations services.AddControllers(); - services.AddShardingDbContext( + services.AddShardingDbContext( o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBMigration;Integrated Security=True;") .ReplaceService>() diff --git a/samples/Sample.MySql/DIExtension.cs b/samples/Sample.MySql/DIExtension.cs index a46d8133..fd664d54 100644 --- a/samples/Sample.MySql/DIExtension.cs +++ b/samples/Sample.MySql/DIExtension.cs @@ -30,7 +30,7 @@ namespace Sample.MySql { using (var scope=app.ApplicationServices.CreateScope()) { - var virtualDbContext =scope.ServiceProvider.GetService(); + var virtualDbContext =scope.ServiceProvider.GetService(); if (!virtualDbContext.Set().Any()) { var ids = Enumerable.Range(1, 1000); diff --git a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs index c5023abc..507366d9 100644 --- a/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.MySql/DbContexts/DefaultShardingDbContext.cs @@ -1,11 +1,13 @@ using System; using Microsoft.EntityFrameworkCore; using Sample.MySql.Domain.Maps; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; namespace Sample.MySql.DbContexts { - public class DefaultShardingDbContext:AbstractShardingDbContext + public class DefaultShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext { public DefaultShardingDbContext(DbContextOptions options) : base(options) { @@ -19,5 +21,6 @@ namespace Sample.MySql.DbContexts modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap()); } + public IRouteTail RouteTail { get; set; } } } diff --git a/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs b/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs deleted file mode 100644 index 1b25a8d8..00000000 --- a/samples/Sample.MySql/DbContexts/DefaultTableDbContext.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Sample.MySql.Domain.Maps; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding.Abstractions; - -namespace Sample.MySql.DbContexts -{ - public class DefaultTableDbContext: DbContext,IShardingTableDbContext - { - public DefaultTableDbContext(DbContextOptions options) :base(options) - { - - } - - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new SysUserModMap()); - modelBuilder.ApplyConfiguration(new SysTestMap()); - modelBuilder.ApplyConfiguration(new SysUserLogByMonthMap()); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs index a43857e5..3a0d74f1 100644 --- a/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.SqlServer/DbContexts/DefaultShardingDbContext.cs @@ -4,11 +4,13 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Sample.SqlServer.Domain.Maps; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; namespace Sample.SqlServer.DbContexts { - public class DefaultShardingDbContext:AbstractShardingDbContext + public class DefaultShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext { public DefaultShardingDbContext(DbContextOptions options) : base(options) { @@ -17,11 +19,11 @@ namespace Sample.SqlServer.DbContexts protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); - //配置默认和DefaultTableDbContext一样 modelBuilder.ApplyConfiguration(new SysUserModMap()); modelBuilder.ApplyConfiguration(new SysTestMap()); modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); } - + + public IRouteTail RouteTail { get; set; } } } diff --git a/samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs b/samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs deleted file mode 100644 index 5afb9c88..00000000 --- a/samples/Sample.SqlServer/DbContexts/DefaultTableDbContext.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Sample.SqlServer.Domain.Maps; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding.Abstractions; - -namespace Sample.SqlServer.DbContexts -{ - public class DefaultTableDbContext: DbContext, IShardingTableDbContext - { - public DefaultTableDbContext(DbContextOptions options) :base(options) - { - - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new SysUserModMap()); - modelBuilder.ApplyConfiguration(new SysTestMap()); - modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/samples/Sample.SqlServer/Startup.cs b/samples/Sample.SqlServer/Startup.cs index c0f7cabc..e56d3909 100644 --- a/samples/Sample.SqlServer/Startup.cs +++ b/samples/Sample.SqlServer/Startup.cs @@ -31,7 +31,7 @@ namespace Sample.SqlServer services.AddControllers(); //services.AddDbContext(o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx3;Integrated Security=True")); - services.AddShardingDbContext( + services.AddShardingDbContext( o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDB1;Integrated Security=True;") ).Begin(o => diff --git a/samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultDbContext.cs b/samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultDbContext.cs deleted file mode 100644 index 9b7e9c05..00000000 --- a/samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultDbContext.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; -using Sample.SqlServerShardingDataSource.Domain.Maps; - -namespace Sample.SqlServerShardingDataSource.DbContexts -{ - public class DefaultDbContext: DbContext - { - public DefaultDbContext(DbContextOptions options) : base(options) - { - - } - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new SysUserModMap()); - } - - } -} diff --git a/samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultShardingDbContext.cs b/samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultShardingDbContext.cs index afba1c46..8f1d33e2 100644 --- a/samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Sample.SqlServerShardingDataSource/DbContexts/DefaultShardingDbContext.cs @@ -8,7 +8,7 @@ using ShardingCore.Sharding; namespace Sample.SqlServerShardingDataSource.DbContexts { - public class DefaultShardingDbContext:AbstractShardingDbContext + public class DefaultShardingDbContext:AbstractShardingDbContext { public DefaultShardingDbContext(DbContextOptions options) : base(options) { diff --git a/samples/Sample.SqlServerShardingDataSource/Startup.cs b/samples/Sample.SqlServerShardingDataSource/Startup.cs index 6103acbb..15e63474 100644 --- a/samples/Sample.SqlServerShardingDataSource/Startup.cs +++ b/samples/Sample.SqlServerShardingDataSource/Startup.cs @@ -36,7 +36,7 @@ namespace Sample.SqlServerShardingDataSource services.AddControllers(); - services.AddShardingDbContext( + services.AddShardingDbContext( o => o.UseSqlServer("Data Source=localhost;Initial Catalog=ShardingCoreDBxx0;Integrated Security=True;") ).Begin(o => diff --git a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs index 850d9f6b..7ee69a8c 100644 --- a/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs +++ b/samples/Samples.AbpSharding/AbstractShardingAbpDbContext.cs @@ -21,23 +21,18 @@ using Volo.Abp.EntityFrameworkCore; namespace Samples.AbpSharding { - public abstract class AbstractShardingAbpDbContext : AbpDbContext>, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite where TDbContext : DbContext + public abstract class AbstractShardingAbpDbContext : AbpDbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite { private readonly IShardingDbContextExecutor _shardingDbContextExecutor; - protected AbstractShardingAbpDbContext(DbContextOptions> options) : base(options) + protected AbstractShardingAbpDbContext(DbContextOptions options) : base(options) { - ActualDbContextType = typeof(TDbContext); _shardingDbContextExecutor = (IShardingDbContextExecutor)Activator.CreateInstance( - typeof(ShardingDbContextExecutor<,>).GetGenericType1(this.GetType(), ActualDbContextType)); + typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType())); } - /// - /// 正真执行的dbcontext类型 - /// - public Type ActualDbContextType { get; } /// /// 读写分离优先级 /// @@ -54,11 +49,19 @@ namespace Samples.AbpSharding get => _shardingDbContextExecutor.ReadWriteSeparation; set => _shardingDbContextExecutor.ReadWriteSeparation = value; } + + public new bool IsExecutor { get; private set; } + + public void ShardingUpgrade() + { + IsExecutor = true; + } + public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail) { var dbContext = _shardingDbContextExecutor.CreateDbContext(parallelQuery, dataSourceName, routeTail); if (!parallelQuery) - ((AbpDbContext)dbContext).LazyServiceProvider = this.LazyServiceProvider; + ((AbpDbContext)dbContext).LazyServiceProvider = this.LazyServiceProvider; return dbContext; } diff --git a/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs b/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs index 46595404..8e734911 100644 --- a/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs +++ b/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultShardingDbContext.cs @@ -1,11 +1,13 @@ using System; using Microsoft.EntityFrameworkCore; using Samples.AutoByDate.SqlServer.Domain.Maps; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; namespace Samples.AutoByDate.SqlServer.DbContexts { - public class DefaultShardingDbContext:AbstractShardingDbContext + public class DefaultShardingDbContext:AbstractShardingDbContext, IShardingTableDbContext { public DefaultShardingDbContext(DbContextOptions options) : base(options) { @@ -18,5 +20,6 @@ namespace Samples.AutoByDate.SqlServer.DbContexts modelBuilder.ApplyConfiguration(new TestLogByWeekMap()); } + public IRouteTail RouteTail { get; set; } } } diff --git a/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultTableDbContext.cs b/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultTableDbContext.cs deleted file mode 100644 index c73355d6..00000000 --- a/samples/Samples.AutoByDate.SqlServer/DbContexts/DefaultTableDbContext.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Samples.AutoByDate.SqlServer.Domain.Maps; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding.Abstractions; - -namespace Samples.AutoByDate.SqlServer.DbContexts -{ - public class DefaultTableDbContext: DbContext,IShardingTableDbContext - { - public DefaultTableDbContext(DbContextOptions options) :base(options) - { - - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new SysUserLogByDayMap()); - modelBuilder.ApplyConfiguration(new TestLogByWeekMap()); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/samples/Samples.AutoByDate.SqlServer/Startup.cs b/samples/Samples.AutoByDate.SqlServer/Startup.cs index 0ae220b5..a20d6b5a 100644 --- a/samples/Samples.AutoByDate.SqlServer/Startup.cs +++ b/samples/Samples.AutoByDate.SqlServer/Startup.cs @@ -34,7 +34,7 @@ namespace Samples.AutoByDate.SqlServer services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo {Title = "Samples.AutoByDate.SqlServer", Version = "v1"}); }); - services.AddShardingDbContext( + services.AddShardingDbContext( o => o.UseSqlServer( "Data Source=localhost;Initial Catalog=ShardingCoreDBxx2;Integrated Security=True;") ).Begin(o => diff --git a/src/ShardingCore/DIExtension.cs b/src/ShardingCore/DIExtension.cs index 2e14ce7b..869a70de 100644 --- a/src/ShardingCore/DIExtension.cs +++ b/src/ShardingCore/DIExtension.cs @@ -35,15 +35,14 @@ namespace ShardingCore */ public static class DIExtension { - public static ShardingCoreConfigBuilder AddShardingDbContext(this IServiceCollection services, + public static ShardingCoreConfigBuilder AddShardingDbContext(this IServiceCollection services, Action optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + where TShardingDbContext : DbContext, IShardingDbContext { - ShardingCoreHelper.CheckContextConstructors(); + ShardingCoreHelper.CheckContextConstructors(); Action shardingOptionAction = option => { optionsAction?.Invoke(option); @@ -51,17 +50,16 @@ namespace ShardingCore }; services.AddDbContext(shardingOptionAction, contextLifetime, optionsLifetime); - return new ShardingCoreConfigBuilder(services); + return new ShardingCoreConfigBuilder(services); } - public static ShardingCoreConfigBuilder AddShardingDbContext(this IServiceCollection services, + public static ShardingCoreConfigBuilder AddShardingDbContext(this IServiceCollection services, Action optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) - where TActualDbContext : DbContext, IShardingTableDbContext - where TShardingDbContext : DbContext, IShardingDbContext + where TShardingDbContext : DbContext, IShardingDbContext { - ShardingCoreHelper.CheckContextConstructors(); + ShardingCoreHelper.CheckContextConstructors(); Action shardingOptionAction = (sp, option) => @@ -70,7 +68,7 @@ namespace ShardingCore option.UseSharding(); }; services.AddDbContext(shardingOptionAction, contextLifetime, optionsLifetime); - return new ShardingCoreConfigBuilder(services); + return new ShardingCoreConfigBuilder(services); } //public static IServiceCollection AddShardingDbContext(this IServiceCollection services, // Action optionsAction = null, diff --git a/src/ShardingCore/DIExtensions/ShardingCoreConfigBuilder.cs b/src/ShardingCore/DIExtensions/ShardingCoreConfigBuilder.cs index c8ae5a15..58ec8bbd 100644 --- a/src/ShardingCore/DIExtensions/ShardingCoreConfigBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingCoreConfigBuilder.cs @@ -13,9 +13,8 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingCoreConfigBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingCoreConfigBuilder + where TShardingDbContext : DbContext, IShardingDbContext { public IServiceCollection Services { get; } @@ -31,7 +30,7 @@ namespace ShardingCore.DIExtensions } - public ShardingQueryBuilder Begin(Action shardingCoreBeginOptionsConfigure) + public ShardingQueryBuilder Begin(Action shardingCoreBeginOptionsConfigure) { var shardingCoreBeginOptions = new ShardingCoreBeginOptions(); shardingCoreBeginOptionsConfigure?.Invoke(shardingCoreBeginOptions); @@ -47,7 +46,7 @@ namespace ShardingCore.DIExtensions ShardingConfigOption.ParallelQueryTimeOut = shardingCoreBeginOptions.ParallelQueryTimeOut; ShardingConfigOption.CreateShardingTableOnStart = shardingCoreBeginOptions.CreateShardingTableOnStart; ShardingConfigOption.IgnoreCreateTableError = shardingCoreBeginOptions.IgnoreCreateTableError; - return new ShardingQueryBuilder(this); + return new ShardingQueryBuilder(this); } //public ShardingCoreConfigBuilder AddDefaultDataSource(string dataSourceName, string connectionString) //{ diff --git a/src/ShardingCore/DIExtensions/ShardingCoreConfigEndBuilder.cs b/src/ShardingCore/DIExtensions/ShardingCoreConfigEndBuilder.cs index 0f26d0c8..d28f2463 100644 --- a/src/ShardingCore/DIExtensions/ShardingCoreConfigEndBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingCoreConfigEndBuilder.cs @@ -19,14 +19,13 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingCoreConfigEndBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingCoreConfigEndBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; public ShardingCoreConfigEndBuilder( - ShardingCoreConfigBuilder shardingCoreConfigBuilder) + ShardingCoreConfigBuilder shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } @@ -49,9 +48,8 @@ namespace ShardingCore.DIExtensions //添加创建TActualDbContext创建者 services .AddSingleton>(sp => - new DefaultShardingDbContextCreatorConfig( - typeof(TActualDbContext))); + DefaultShardingDbContextCreatorConfig>(sp => + new DefaultShardingDbContextCreatorConfig()); if (!_shardingCoreConfigBuilder.ShardingConfigOption.UseReadWrite) { diff --git a/src/ShardingCore/DIExtensions/ShardingDataBaseOrTableBuilder.cs b/src/ShardingCore/DIExtensions/ShardingDataBaseOrTableBuilder.cs index 65aa45fe..f0e96de8 100644 --- a/src/ShardingCore/DIExtensions/ShardingDataBaseOrTableBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingDataBaseOrTableBuilder.cs @@ -13,24 +13,23 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingDataBaseOrTableBuilder: ShardingReadWriteSeparationBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingDataBaseOrTableBuilder: ShardingReadWriteSeparationBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; - public ShardingDataBaseOrTableBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder):base(shardingCoreConfigBuilder) + public ShardingDataBaseOrTableBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder):base(shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } - public ShardingDataSourceRouteBuilder AddShardingDataSource(Func> dataSourcesConfigure) + public ShardingDataSourceRouteBuilder AddShardingDataSource(Func> dataSourcesConfigure) { _shardingCoreConfigBuilder.ShardingConfigOption.AddShardingDataSource(dataSourcesConfigure); - return new ShardingDataSourceRouteBuilder(_shardingCoreConfigBuilder); + return new ShardingDataSourceRouteBuilder(_shardingCoreConfigBuilder); } - public ShardingReadWriteSeparationBuilder AddShardingTableRoute(Action shardingTableConfigure) + public ShardingReadWriteSeparationBuilder AddShardingTableRoute(Action shardingTableConfigure) { var shardingTableOptions = new ShardingTableOptions(); shardingTableConfigure.Invoke(shardingTableOptions); @@ -39,7 +38,7 @@ namespace ShardingCore.DIExtensions { _shardingCoreConfigBuilder.ShardingConfigOption.AddShardingTableRoute(shardingTableRoute); } - return new ShardingReadWriteSeparationBuilder(_shardingCoreConfigBuilder); + return new ShardingReadWriteSeparationBuilder(_shardingCoreConfigBuilder); } } } diff --git a/src/ShardingCore/DIExtensions/ShardingDataSourceRouteBuilder.cs b/src/ShardingCore/DIExtensions/ShardingDataSourceRouteBuilder.cs index c38280bc..ad99c49b 100644 --- a/src/ShardingCore/DIExtensions/ShardingDataSourceRouteBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingDataSourceRouteBuilder.cs @@ -13,18 +13,17 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingDataSourceRouteBuilder : ShardingReadWriteSeparationBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingDataSourceRouteBuilder : ShardingReadWriteSeparationBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; - public ShardingDataSourceRouteBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder) + public ShardingDataSourceRouteBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } - public ShardingTableBuilder AddShardingDataSourceRoute(Action shardingDatabaseConfigure) + public ShardingTableBuilder AddShardingDataSourceRoute(Action shardingDatabaseConfigure) { var shardingDatabaseOptions = new ShardingDatabaseOptions(); @@ -35,7 +34,7 @@ namespace ShardingCore.DIExtensions _shardingCoreConfigBuilder.ShardingConfigOption.AddShardingDataSourceRoute(shardingDatabaseRoute); } - return new ShardingTableBuilder(_shardingCoreConfigBuilder); + return new ShardingTableBuilder(_shardingCoreConfigBuilder); } } } diff --git a/src/ShardingCore/DIExtensions/ShardingDefaultDataSourceBuilder.cs b/src/ShardingCore/DIExtensions/ShardingDefaultDataSourceBuilder.cs index 3a3f6a37..0ba248cb 100644 --- a/src/ShardingCore/DIExtensions/ShardingDefaultDataSourceBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingDefaultDataSourceBuilder.cs @@ -13,23 +13,22 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingDefaultDataSourceBuilder: ShardingCoreConfigEndBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingDefaultDataSourceBuilder: ShardingCoreConfigEndBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; - public ShardingDefaultDataSourceBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder):base(shardingCoreConfigBuilder) + public ShardingDefaultDataSourceBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder):base(shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } - public ShardingDataBaseOrTableBuilder AddDefaultDataSource(string dataSourceName, string connectionString) + public ShardingDataBaseOrTableBuilder AddDefaultDataSource(string dataSourceName, string connectionString) { if (!string.IsNullOrWhiteSpace(_shardingCoreConfigBuilder.ShardingConfigOption.DefaultDataSourceName) || !string.IsNullOrWhiteSpace(_shardingCoreConfigBuilder.ShardingConfigOption.DefaultConnectionString)) throw new InvalidOperationException($"{nameof(AddDefaultDataSource)}-{dataSourceName}"); _shardingCoreConfigBuilder.ShardingConfigOption.DefaultDataSourceName = dataSourceName; _shardingCoreConfigBuilder.ShardingConfigOption.DefaultConnectionString = connectionString; - return new ShardingDataBaseOrTableBuilder(_shardingCoreConfigBuilder); + return new ShardingDataBaseOrTableBuilder(_shardingCoreConfigBuilder); } } } diff --git a/src/ShardingCore/DIExtensions/ShardingQueryBuilder.cs b/src/ShardingCore/DIExtensions/ShardingQueryBuilder.cs index e237ec05..ebeb0dfc 100644 --- a/src/ShardingCore/DIExtensions/ShardingQueryBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingQueryBuilder.cs @@ -14,20 +14,19 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingQueryBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingQueryBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; - public ShardingQueryBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) + public ShardingQueryBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } - public ShardingTransactionBuilder AddShardingQuery(Action queryConfigure) + public ShardingTransactionBuilder AddShardingQuery(Action queryConfigure) { _shardingCoreConfigBuilder.ShardingConfigOption.UseShardingQuery(queryConfigure); - return new ShardingTransactionBuilder(_shardingCoreConfigBuilder); + return new ShardingTransactionBuilder(_shardingCoreConfigBuilder); } } } diff --git a/src/ShardingCore/DIExtensions/ShardingReadWriteSeparationBuilder.cs b/src/ShardingCore/DIExtensions/ShardingReadWriteSeparationBuilder.cs index 8da23f63..16311c64 100644 --- a/src/ShardingCore/DIExtensions/ShardingReadWriteSeparationBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingReadWriteSeparationBuilder.cs @@ -14,18 +14,17 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingReadWriteSeparationBuilder : ShardingCoreConfigEndBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingReadWriteSeparationBuilder : ShardingCoreConfigEndBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; - public ShardingReadWriteSeparationBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder) + public ShardingReadWriteSeparationBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } - public ShardingCoreConfigEndBuilder AddReadWriteSeparation( + public ShardingCoreConfigEndBuilder AddReadWriteSeparation( Func>> readWriteSeparationConfigure, ReadStrategyEnum readStrategyEnum, bool defaultEnable = false, @@ -33,7 +32,7 @@ namespace ShardingCore.DIExtensions ReadConnStringGetStrategyEnum readConnStringGetStrategy = ReadConnStringGetStrategyEnum.LatestFirstTime) { _shardingCoreConfigBuilder.ShardingConfigOption.UseReadWriteConfiguration(readWriteSeparationConfigure,readStrategyEnum, defaultEnable,defaultPriority); - return new ShardingCoreConfigEndBuilder(_shardingCoreConfigBuilder); + return new ShardingCoreConfigEndBuilder(_shardingCoreConfigBuilder); } } } diff --git a/src/ShardingCore/DIExtensions/ShardingTableBuilder.cs b/src/ShardingCore/DIExtensions/ShardingTableBuilder.cs index 55d3d1ee..5c43a00f 100644 --- a/src/ShardingCore/DIExtensions/ShardingTableBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingTableBuilder.cs @@ -13,18 +13,17 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingTableBuilder : ShardingReadWriteSeparationBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingTableBuilder : ShardingReadWriteSeparationBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; - public ShardingTableBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder) + public ShardingTableBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) : base(shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } - public ShardingReadWriteSeparationBuilder AddShardingTableRoute(Action shardingTableConfigure) + public ShardingReadWriteSeparationBuilder AddShardingTableRoute(Action shardingTableConfigure) { var shardingTableOptions = new ShardingTableOptions(); @@ -34,7 +33,7 @@ namespace ShardingCore.DIExtensions { _shardingCoreConfigBuilder.ShardingConfigOption.AddShardingTableRoute(shardingTableRoute); } - return new ShardingReadWriteSeparationBuilder(_shardingCoreConfigBuilder); + return new ShardingReadWriteSeparationBuilder(_shardingCoreConfigBuilder); } } } diff --git a/src/ShardingCore/DIExtensions/ShardingTransactionBuilder.cs b/src/ShardingCore/DIExtensions/ShardingTransactionBuilder.cs index d5dae339..a8ec29f1 100644 --- a/src/ShardingCore/DIExtensions/ShardingTransactionBuilder.cs +++ b/src/ShardingCore/DIExtensions/ShardingTransactionBuilder.cs @@ -14,20 +14,19 @@ namespace ShardingCore.DIExtensions * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingTransactionBuilder - where TActualDbContext : DbContext - where TShardingDbContext : DbContext, IShardingDbContext + public class ShardingTransactionBuilder + where TShardingDbContext : DbContext, IShardingDbContext { - private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; + private readonly ShardingCoreConfigBuilder _shardingCoreConfigBuilder; - public ShardingTransactionBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) + public ShardingTransactionBuilder(ShardingCoreConfigBuilder shardingCoreConfigBuilder) { _shardingCoreConfigBuilder = shardingCoreConfigBuilder; } - public ShardingDefaultDataSourceBuilder AddShardingTransaction(Action transactionConfigure) + public ShardingDefaultDataSourceBuilder AddShardingTransaction(Action transactionConfigure) { _shardingCoreConfigBuilder.ShardingConfigOption.UseShardingTransaction(transactionConfigure); - return new ShardingDefaultDataSourceBuilder(_shardingCoreConfigBuilder); + return new ShardingDefaultDataSourceBuilder(_shardingCoreConfigBuilder); } } } diff --git a/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs b/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs index 02b02e60..8c74f19c 100644 --- a/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs +++ b/src/ShardingCore/DbContexts/ShardingDbContextFactory.cs @@ -35,6 +35,15 @@ namespace ShardingCore.DbContexts { shardingTableDbContext.RouteTail = routeTail; } + + if (dbContext is IShardingDbContext shardingDbContext) + { + shardingDbContext.ShardingUpgrade(); + } + else + { + throw new ShardingCoreException($"{dbContext.GetType().FullName} should implements {nameof(IShardingDbContext)}"); + } var dbContextModel = dbContext.Model; return dbContext; } diff --git a/src/ShardingCore/DefaultShardingDbContextCreatorConfig.cs b/src/ShardingCore/DefaultShardingDbContextCreatorConfig.cs index 2d589cdb..43237571 100644 --- a/src/ShardingCore/DefaultShardingDbContextCreatorConfig.cs +++ b/src/ShardingCore/DefaultShardingDbContextCreatorConfig.cs @@ -13,19 +13,16 @@ namespace ShardingCore * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class DefaultShardingDbContextCreatorConfig : IShardingDbContextCreatorConfig + public class DefaultShardingDbContextCreatorConfig : IShardingDbContextCreatorConfig where TShardingDbContext : DbContext, IShardingDbContext - where TActualDbContext : DbContext { private readonly Func _creator; - public DefaultShardingDbContextCreatorConfig(Type actualDbContextType) + public DefaultShardingDbContextCreatorConfig() { - ActualDbContextType = actualDbContextType; - _creator = ShardingCoreHelper.CreateActivator(); + _creator = ShardingCoreHelper.CreateActivator(); } public Type ShardingDbContextType => typeof(TShardingDbContext); - public Type ActualDbContextType { get; } public DbContext Creator(ShardingDbContextOptions shardingDbContextOptions) { return _creator(shardingDbContextOptions); diff --git a/src/ShardingCore/EFCores/ShardingInternalDbSet.cs b/src/ShardingCore/EFCores/ShardingInternalDbSet.cs index 9e3d3ef4..e846a281 100644 --- a/src/ShardingCore/EFCores/ShardingInternalDbSet.cs +++ b/src/ShardingCore/EFCores/ShardingInternalDbSet.cs @@ -22,19 +22,19 @@ namespace ShardingCore.EFCores public class ShardingInternalDbSet : InternalDbSet where TEntity : class { - private readonly DbContext _context; + private readonly IShardingDbContext _context; #if EFCORE5 public ShardingInternalDbSet(DbContext context, string entityTypeName) : base(context, entityTypeName) { - _context = context; + _context = (IShardingDbContext)context; } #endif #if !EFCORE5 public ShardingInternalDbSet(DbContext context) : base(context) { - _context = context; + _context = (IShardingDbContext)context; } #endif /// @@ -45,7 +45,7 @@ namespace ShardingCore.EFCores /// public override EntityEntry Add(TEntity entity) { - var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity); + var genericDbContext = _context.CreateGenericDbContext(entity); return genericDbContext.Set().Add(entity); } @@ -60,7 +60,7 @@ namespace ShardingCore.EFCores TEntity entity, CancellationToken cancellationToken = default) { - var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity); + var genericDbContext = _context.CreateGenericDbContext(entity); return await genericDbContext.Set().AddAsync(entity, cancellationToken); } @@ -68,7 +68,7 @@ namespace ShardingCore.EFCores #if EFCORE2 public override async Task> AddAsync(TEntity entity, CancellationToken cancellationToken = new CancellationToken()) { - var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity); + var genericDbContext = _context.CreateGenericDbContext(entity); return await genericDbContext.Set().AddAsync(entity, cancellationToken); } @@ -82,7 +82,7 @@ namespace ShardingCore.EFCores /// public override EntityEntry Attach(TEntity entity) { - var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity); + var genericDbContext = _context.CreateGenericDbContext(entity); return genericDbContext.Set().Attach(entity); } @@ -96,7 +96,7 @@ namespace ShardingCore.EFCores { Check.NotNull(entity, nameof(entity)); - var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity); + var genericDbContext = _context.CreateGenericDbContext(entity); return genericDbContext.Set().Remove(entity); } @@ -108,7 +108,7 @@ namespace ShardingCore.EFCores /// public override EntityEntry Update(TEntity entity) { - var genericDbContext = ((IShardingDbContext)_context).CreateGenericDbContext(entity); + var genericDbContext = _context.CreateGenericDbContext(entity); return genericDbContext.Set().Update(entity); } @@ -123,7 +123,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -148,7 +148,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -172,7 +172,7 @@ namespace ShardingCore.EFCores { var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -198,7 +198,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -223,7 +223,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -248,7 +248,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -275,7 +275,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -300,7 +300,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -328,7 +328,7 @@ namespace ShardingCore.EFCores var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, @@ -352,7 +352,7 @@ namespace ShardingCore.EFCores { var groups = entities.Select(o => { - var dbContext = ((IShardingDbContext)_context).CreateGenericDbContext(o); + var dbContext = _context.CreateGenericDbContext(o); return new { DbContext = dbContext, diff --git a/src/ShardingCore/IShardingDbContextCreatorConfig.cs b/src/ShardingCore/IShardingDbContextCreatorConfig.cs index 707bd8bc..319eeb61 100644 --- a/src/ShardingCore/IShardingDbContextCreatorConfig.cs +++ b/src/ShardingCore/IShardingDbContextCreatorConfig.cs @@ -17,7 +17,6 @@ namespace ShardingCore public interface IShardingDbContextCreatorConfig { Type ShardingDbContextType { get; } - Type ActualDbContextType { get; } DbContext Creator(ShardingDbContextOptions shardingDbContextOptions); } diff --git a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs index 19244a79..e874214d 100644 --- a/src/ShardingCore/Sharding/AbstractShardingDbContext.cs +++ b/src/ShardingCore/Sharding/AbstractShardingDbContext.cs @@ -20,29 +20,24 @@ namespace ShardingCore.Sharding * @Date: Saturday, 14 August 2021 09:57:08 * @Email: 326308290@qq.com */ + /// /// 分表分库的dbcontext /// - /// - public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite where TDbContext : DbContext + public abstract class AbstractShardingDbContext : DbContext, IShardingDbContext, ISupportShardingTransaction, ISupportShardingReadWrite { private readonly IShardingDbContextExecutor _shardingDbContextExecutor; public AbstractShardingDbContext(DbContextOptions options) : base(options) { - ActualDbContextType = typeof(TDbContext); _shardingDbContextExecutor = (IShardingDbContextExecutor)Activator.CreateInstance( - typeof(ShardingDbContextExecutor<,>).GetGenericType1(this.GetType(), ActualDbContextType)); + typeof(ShardingDbContextExecutor<>).GetGenericType0(this.GetType())); } /// - /// 正真执行的dbcontext类型 - /// - public Type ActualDbContextType { get; } - /// /// 读写分离优先级 /// public int ReadWriteSeparationPriority @@ -58,6 +53,16 @@ namespace ShardingCore.Sharding get => _shardingDbContextExecutor.ReadWriteSeparation; set => _shardingDbContextExecutor.ReadWriteSeparation = value; } + /// + /// 是否是真正的执行者 + /// + public new bool IsExecutor { get; private set; } + + public void ShardingUpgrade() + { + IsExecutor = true; + } + public DbContext GetDbContext(string dataSourceName, bool parallelQuery, IRouteTail routeTail) { return _shardingDbContextExecutor.CreateDbContext(parallelQuery, dataSourceName, routeTail); @@ -77,11 +82,15 @@ namespace ShardingCore.Sharding public override EntityEntry Add(object entity) { + if (IsExecutor) + base.Add(entity); return CreateGenericDbContext(entity).Add(entity); } public override EntityEntry Add(TEntity entity) { + if (IsExecutor) + return base.Add(entity); return CreateGenericDbContext(entity).Add(entity); } @@ -91,28 +100,41 @@ namespace ShardingCore.Sharding public override ValueTask> AddAsync(TEntity entity, CancellationToken cancellationToken = new CancellationToken()) { + if (IsExecutor) + return base.AddAsync(entity, cancellationToken); return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken); } public override ValueTask AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken()) { + if (IsExecutor) + return base.AddAsync(entity, cancellationToken); return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken); } #endif #if EFCORE2 public override Task> AddAsync(TEntity entity, CancellationToken cancellationToken = new CancellationToken()) { + if (IsExecutor) + return base.AddAsync(entity, cancellationToken); return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken); } public override Task AddAsync(object entity, CancellationToken cancellationToken = new CancellationToken()) { + if (IsExecutor) + return base.AddAsync(entity, cancellationToken); return CreateGenericDbContext(entity).AddAsync(entity, cancellationToken); } #endif public override void AddRange(params object[] entities) { + if (IsExecutor) + { + base.AddRange(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -131,6 +153,11 @@ namespace ShardingCore.Sharding public override void AddRange(IEnumerable entities) { + if (IsExecutor) + { + base.AddRange(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -149,6 +176,11 @@ namespace ShardingCore.Sharding public override async Task AddRangeAsync(params object[] entities) { + if (IsExecutor) + { + await base.AddRangeAsync(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -167,6 +199,11 @@ namespace ShardingCore.Sharding public override async Task AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = new CancellationToken()) { + if (IsExecutor) + { + await base.AddRangeAsync(entities, cancellationToken); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -185,16 +222,25 @@ namespace ShardingCore.Sharding public override EntityEntry Attach(TEntity entity) { + if (IsExecutor) + return base.Attach(entity); return CreateGenericDbContext(entity).Attach(entity); } public override EntityEntry Attach(object entity) { + if (IsExecutor) + return base.Attach(entity); return CreateGenericDbContext(entity).Attach(entity); } public override void AttachRange(params object[] entities) { + if (IsExecutor) + { + base.AttachRange(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -213,6 +259,11 @@ namespace ShardingCore.Sharding public override void AttachRange(IEnumerable entities) { + if (IsExecutor) + { + base.AttachRange(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -236,26 +287,39 @@ namespace ShardingCore.Sharding public override EntityEntry Entry(TEntity entity) { + if (IsExecutor) + return base.Entry(entity); return CreateGenericDbContext(entity).Entry(entity); } public override EntityEntry Entry(object entity) { + if (IsExecutor) + return base.Entry(entity); return CreateGenericDbContext(entity).Entry(entity); } public override EntityEntry Update(TEntity entity) { + if (IsExecutor) + return base.Update(entity); return CreateGenericDbContext(entity).Update(entity); } public override EntityEntry Update(object entity) { + if (IsExecutor) + return base.Update(entity); return CreateGenericDbContext(entity).Update(entity); } public override void UpdateRange(params object[] entities) { + if (IsExecutor) + { + base.UpdateRange(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -274,6 +338,11 @@ namespace ShardingCore.Sharding public override void UpdateRange(IEnumerable entities) { + if (IsExecutor) + { + base.UpdateRange(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -292,17 +361,26 @@ namespace ShardingCore.Sharding public override EntityEntry Remove(TEntity entity) { + if (IsExecutor) + return base.Remove(entity); return CreateGenericDbContext(entity).Remove(entity); } public override EntityEntry Remove(object entity) { + if (IsExecutor) + return base.Remove(entity); return CreateGenericDbContext(entity).Remove(entity); } public override void RemoveRange(params object[] entities) { - var groups = entities.Select(o => + if (IsExecutor) + { + base.RemoveRange(entities); + return; + } + var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); return new @@ -320,6 +398,11 @@ namespace ShardingCore.Sharding public override void RemoveRange(IEnumerable entities) { + if (IsExecutor) + { + base.RemoveRange(entities); + return; + } var groups = entities.Select(o => { var dbContext = CreateGenericDbContext(o); @@ -368,11 +451,16 @@ namespace ShardingCore.Sharding //} public override int SaveChanges() { + + if (IsExecutor) + return base.SaveChanges(); return this.SaveChanges(true); } public override int SaveChanges(bool acceptAllChangesOnSuccess) { + if (IsExecutor) + return base.SaveChanges(acceptAllChangesOnSuccess); //ApplyShardingConcepts(); int i = 0; //如果是内部开的事务就内部自己消化 @@ -395,19 +483,23 @@ namespace ShardingCore.Sharding public override Task SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken()) { + if (IsExecutor) + return base.SaveChangesAsync(cancellationToken); return this.SaveChangesAsync(true, cancellationToken); } public override async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken()) { + if (IsExecutor) + return await base.SaveChangesAsync(acceptAllChangesOnSuccess,cancellationToken); //ApplyShardingConcepts(); int i = 0; //如果是内部开的事务就内部自己消化 if (!_shardingDbContextExecutor.IsBeginTransaction) { - using(var tran= _shardingDbContextExecutor.BeginTransaction()) + using (var tran = _shardingDbContextExecutor.BeginTransaction()) { - i = await _shardingDbContextExecutor.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); + i = await _shardingDbContextExecutor.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); #if EFCORE2 tran.Commit(); #endif @@ -427,16 +519,31 @@ namespace ShardingCore.Sharding public override void Dispose() { - _shardingDbContextExecutor.Dispose(); - base.Dispose(); + + if (IsExecutor) + { + base.Dispose(); + } + else + { + _shardingDbContextExecutor.Dispose(); + base.Dispose(); + } } #if !EFCORE2 public override async ValueTask DisposeAsync() { - await _shardingDbContextExecutor.DisposeAsync(); + if (IsExecutor) + { + await base.DisposeAsync(); + } + else + { + await _shardingDbContextExecutor.DisposeAsync(); - await base.DisposeAsync(); + await base.DisposeAsync(); + } } #endif diff --git a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs index ddda6ed1..adc3e70c 100644 --- a/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs +++ b/src/ShardingCore/Sharding/Abstractions/IShardingDbContext.cs @@ -13,9 +13,14 @@ namespace ShardingCore.Sharding.Abstractions public interface IShardingDbContext { /// - /// 真实的db context type + /// 是否是执行者 /// - Type ActualDbContextType { get;} + bool IsExecutor { get; } + /// + /// 分片升级为执行者 + /// + /// + void ShardingUpgrade(); /// /// create DbContext /// @@ -33,9 +38,4 @@ namespace ShardingCore.Sharding.Abstractions DbContext CreateGenericDbContext(T entity) where T : class; } - - public interface IShardingDbContext : IShardingDbContext where T : DbContext - { - - } } \ No newline at end of file diff --git a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs index 24b58a91..65dafb2d 100644 --- a/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs +++ b/src/ShardingCore/Sharding/ShardingDbContextExecutors/ShardingDbContextExecutor.cs @@ -29,7 +29,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors /// DbContext执行者 /// /// - public class ShardingDbContextExecutor : IShardingDbContextExecutor where TShardingDbContext : DbContext, IShardingDbContext where TActualDbContext : DbContext + public class ShardingDbContextExecutor : IShardingDbContextExecutor where TShardingDbContext : DbContext, IShardingDbContext { private readonly ConcurrentDictionary> _dbContextCaches = new ConcurrentDictionary>(); public IShardingTransaction CurrentShardingTransaction { get; private set; } @@ -68,14 +68,14 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors #region create db context - private DbContextOptionsBuilder CreateDbContextOptionBuilder() + private DbContextOptionsBuilder CreateDbContextOptionBuilder() { Type type = typeof(DbContextOptionsBuilder<>); - type = type.MakeGenericType(typeof(TActualDbContext)); - return (DbContextOptionsBuilder)Activator.CreateInstance(type); + type = type.MakeGenericType(typeof(TShardingDbContext)); + return (DbContextOptionsBuilder)Activator.CreateInstance(type); } - private DbContextOptions CreateShareDbContextOptions(string dataSourceName) + private DbContextOptions CreateShareDbContextOptions(string dataSourceName) { var dbContextOptionBuilder = CreateDbContextOptionBuilder(); @@ -104,7 +104,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors return new ShardingDbContextOptions(dbContextOptions, routeTail); } - private DbContextOptions CreateParallelDbContextOptions(string dataSourceName) + private DbContextOptions CreateParallelDbContextOptions(string dataSourceName) { var dbContextOptionBuilder = CreateDbContextOptionBuilder(); var connectionString = _actualConnectionStringManager.GetConnectionString(dataSourceName, false); diff --git a/test/ShardingCore.Test50/DefaultDbContext.cs b/test/ShardingCore.Test50/DefaultDbContext.cs deleted file mode 100644 index 3c3d1f62..00000000 --- a/test/ShardingCore.Test50/DefaultDbContext.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.DbContexts.ShardingDbContexts; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Test50.Domain.Maps; - -namespace ShardingCore.Test50 -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/3/31 15:28:11 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - public class DefaultDbContext : DbContext, IShardingTableDbContext - { - public DefaultDbContext(DbContextOptions options) : base(options) - { - - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new SysUserModMap()); - modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/test/ShardingCore.Test50/ShardingDefaultDbContext.cs b/test/ShardingCore.Test50/ShardingDefaultDbContext.cs index f699362c..a3fa63a2 100644 --- a/test/ShardingCore.Test50/ShardingDefaultDbContext.cs +++ b/test/ShardingCore.Test50/ShardingDefaultDbContext.cs @@ -4,7 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; using ShardingCore.Test50.Domain.Maps; namespace ShardingCore.Test50 @@ -16,7 +18,7 @@ namespace ShardingCore.Test50 * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingDefaultDbContext:AbstractShardingDbContext + public class ShardingDefaultDbContext:AbstractShardingDbContext, IShardingTableDbContext { public ShardingDefaultDbContext(DbContextOptions options) : base(options) { @@ -29,5 +31,6 @@ namespace ShardingCore.Test50 modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); } + public IRouteTail RouteTail { get; set; } } } diff --git a/test/ShardingCore.Test50/Startup.cs b/test/ShardingCore.Test50/Startup.cs index f667684a..b0a7d12b 100644 --- a/test/ShardingCore.Test50/Startup.cs +++ b/test/ShardingCore.Test50/Startup.cs @@ -48,7 +48,7 @@ namespace ShardingCore.Test50 // ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services) public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) { - services.AddShardingDbContext(o => + services.AddShardingDbContext(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])) .Begin(o => { diff --git a/test/ShardingCore.Test50_2x/DefaultDbContext.cs b/test/ShardingCore.Test50_2x/DefaultDbContext.cs deleted file mode 100644 index f4cec207..00000000 --- a/test/ShardingCore.Test50_2x/DefaultDbContext.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Test50_2x.Domain.Maps; - -namespace ShardingCore.Test50_2x -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/3/31 15:28:11 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - public class DefaultDbContext : DbContext, IShardingTableDbContext - { - public DefaultDbContext(DbContextOptions options) : base(options) - { - - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new SysUserModMap()); - modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/test/ShardingCore.Test50_2x/ShardingDefaultDbContext.cs b/test/ShardingCore.Test50_2x/ShardingDefaultDbContext.cs index b1db7f59..ce37e238 100644 --- a/test/ShardingCore.Test50_2x/ShardingDefaultDbContext.cs +++ b/test/ShardingCore.Test50_2x/ShardingDefaultDbContext.cs @@ -1,6 +1,8 @@ using System; using Microsoft.EntityFrameworkCore; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; using ShardingCore.Test50_2x.Domain.Maps; namespace ShardingCore.Test50_2x @@ -12,7 +14,7 @@ namespace ShardingCore.Test50_2x * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingDefaultDbContext:AbstractShardingDbContext + public class ShardingDefaultDbContext:AbstractShardingDbContext, IShardingTableDbContext { public ShardingDefaultDbContext(DbContextOptions options) : base(options) { @@ -25,5 +27,6 @@ namespace ShardingCore.Test50_2x modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); } + public IRouteTail RouteTail { get; set; } } } diff --git a/test/ShardingCore.Test50_2x/Startup.cs b/test/ShardingCore.Test50_2x/Startup.cs index 6f2592ed..4706f49b 100644 --- a/test/ShardingCore.Test50_2x/Startup.cs +++ b/test/ShardingCore.Test50_2x/Startup.cs @@ -45,7 +45,7 @@ namespace ShardingCore.Test50_2x { //services.AddDbContext(); - services.AddShardingDbContext(o => + services.AddShardingDbContext(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])) .Begin(o => { diff --git a/test/ShardingCore.Test50_3x/DefaultDbContext.cs b/test/ShardingCore.Test50_3x/DefaultDbContext.cs deleted file mode 100644 index 0dab5e93..00000000 --- a/test/ShardingCore.Test50_3x/DefaultDbContext.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; -using ShardingCore.Sharding.Abstractions; -using ShardingCore.Test50_3x.Domain.Maps; - -namespace ShardingCore.Test50_3x -{ - /* - * @Author: xjm - * @Description: - * @Date: 2021/3/31 15:28:11 - * @Ver: 1.0 - * @Email: 326308290@qq.com - */ - public class DefaultDbContext : DbContext, IShardingTableDbContext - { - public DefaultDbContext(DbContextOptions options) : base(options) - { - - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - modelBuilder.ApplyConfiguration(new SysUserModMap()); - modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); - } - - public IRouteTail RouteTail { get; set; } - } -} diff --git a/test/ShardingCore.Test50_3x/ShardingDefaultDbContext.cs b/test/ShardingCore.Test50_3x/ShardingDefaultDbContext.cs index 23a8bc7b..c6a33f7e 100644 --- a/test/ShardingCore.Test50_3x/ShardingDefaultDbContext.cs +++ b/test/ShardingCore.Test50_3x/ShardingDefaultDbContext.cs @@ -1,6 +1,8 @@ using System; using Microsoft.EntityFrameworkCore; +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; using ShardingCore.Sharding; +using ShardingCore.Sharding.Abstractions; using ShardingCore.Test50_3x.Domain.Maps; namespace ShardingCore.Test50_3x @@ -12,7 +14,7 @@ namespace ShardingCore.Test50_3x * @Ver: 1.0 * @Email: 326308290@qq.com */ - public class ShardingDefaultDbContext:AbstractShardingDbContext + public class ShardingDefaultDbContext:AbstractShardingDbContext, IShardingTableDbContext { public ShardingDefaultDbContext(DbContextOptions options) : base(options) { @@ -25,5 +27,6 @@ namespace ShardingCore.Test50_3x modelBuilder.ApplyConfiguration(new SysUserSalaryMap()); } + public IRouteTail RouteTail { get; set; } } } diff --git a/test/ShardingCore.Test50_3x/Startup.cs b/test/ShardingCore.Test50_3x/Startup.cs index 7323451b..225e35e5 100644 --- a/test/ShardingCore.Test50_3x/Startup.cs +++ b/test/ShardingCore.Test50_3x/Startup.cs @@ -41,7 +41,7 @@ namespace ShardingCore.Test50_3x // ConfigureServices(HostBuilderContext hostBuilderContext, IServiceCollection services) public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext) { - services.AddShardingDbContext(o => + services.AddShardingDbContext(o => o.UseSqlServer(hostBuilderContext.Configuration.GetSection("SqlServer")["ConnectionString"])) .Begin(o => {