33using Microsoft . EntityFrameworkCore . Migrations ;
44using Sample . MySql . DbContexts ;
55using Sample . MySql . Domain . Entities ;
6+ using Sample . MySql . multi ;
67using Sample . MySql . Shardings ;
78using ShardingCore ;
89using ShardingCore . Bootstrappers ;
1112using ShardingCore . EFCores ;
1213using ShardingCore . Extensions ;
1314using ShardingCore . Helpers ;
15+ using ShardingCore . Sharding . ReadWriteConfigurations ;
1416using ShardingCore . TableExists ;
1517using ShardingCore . TableExists . Abstractions ;
1618
@@ -37,8 +39,10 @@ public class Startup
3739 {
3840 public static readonly ILoggerFactory efLogger = LoggerFactory . Create ( builder =>
3941 {
40- builder . AddFilter ( ( category , level ) => category == DbLoggerCategory . Database . Command . Name && level == LogLevel . Information ) . AddConsole ( ) ;
42+ builder . AddFilter ( ( category , level ) =>
43+ category == DbLoggerCategory . Database . Command . Name && level == LogLevel . Information ) . AddConsole ( ) ;
4144 } ) ;
45+
4246 public Startup ( IConfiguration configuration )
4347 {
4448 Configuration = configuration ;
@@ -61,52 +65,90 @@ public void ConfigureServices(IServiceCollection services)
6165 // op.AddShardingTableRoute<SysUserModVirtualTableRoute>();
6266 // op.AddShardingTableRoute<SysUserSalaryVirtualTableRoute>();
6367 // });
64-
68+ services . AddMultiShardingDbContext < OtherDbContext > ( )
69+ . UseRouteConfig ( op => { op . AddShardingTableRoute < MyUserRoute > ( ) ; } )
70+ . UseConfig ( o =>
71+ {
72+ o . ThrowIfQueryRouteNotMatch = false ;
73+ o . UseShardingQuery ( ( conStr , builder ) =>
74+ {
75+ builder . UseMySql ( conStr , new MySqlServerVersion ( new Version ( ) ) )
76+ . UseLoggerFactory ( efLogger )
77+ . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking ) ;
78+ } ) ;
79+ o . UseShardingTransaction ( ( connection , builder ) =>
80+ {
81+ builder
82+ . UseMySql ( connection , new MySqlServerVersion ( new Version ( ) ) )
83+ . UseLoggerFactory ( efLogger )
84+ . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking ) ;
85+ } ) ;
86+ o . UseShardingMigrationConfigure ( b =>
87+ {
88+ b . ReplaceService < IMigrationsSqlGenerator , ShardingMySqlMigrationsSqlGenerator > ( ) ;
89+ } ) ;
90+ o . AddDefaultDataSource ( "ds0" ,
91+ "server=127.0.0.1;port=3306;database=dbdbdx;userid=root;password=root;" ) ;
92+ } ) . AddShardingCore ( ) ;
6593 services . AddSingleton < IShardingRuntimeContext > ( sp =>
6694 {
67- Stopwatch stopwatch = Stopwatch . StartNew ( ) ;
68-
95+ Stopwatch stopwatch = Stopwatch . StartNew ( ) ;
96+
6997 var shardingRuntimeContext = new ShardingRuntimeBuilder < DefaultShardingDbContext > ( )
7098 . UseRouteConfig ( o =>
7199 {
72100 o . AddShardingTableRoute < SysUserLogByMonthRoute > ( ) ;
73101 o . AddShardingTableRoute < SysUserModVirtualTableRoute > ( ) ;
74- o . AddShardingDataSourceRoute < SysUserModVirtualDataSourceRoute > ( ) ;
102+ o . AddShardingDataSourceRoute < SysUserModVirtualDataSourceRoute > ( ) ;
75103 } ) . UseConfig ( o =>
76104 {
77105 o . ThrowIfQueryRouteNotMatch = false ;
78- o . UseShardingQuery ( ( conStr , builder ) =>
106+ o . UseShardingQuery ( ( conStr , builder ) =>
79107 {
80108 builder . UseMySql ( conStr , new MySqlServerVersion ( new Version ( ) ) )
81- . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking )
82109 . UseLoggerFactory ( efLogger )
83- . EnableSensitiveDataLogging ( ) ;
110+ . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking ) ;
84111 } ) ;
85112 o . UseShardingTransaction ( ( connection , builder ) =>
86113 {
87114 builder
88- . UseMySql ( connection , new MySqlServerVersion ( new Version ( ) ) ) .
89- UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking )
115+ . UseMySql ( connection , new MySqlServerVersion ( new Version ( ) ) )
90116 . UseLoggerFactory ( efLogger )
91- . EnableSensitiveDataLogging ( ) ;
117+ . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking ) ;
92118 } ) ;
93- o . AddDefaultDataSource ( "ds0" , "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;" ) ;
94- o . AddExtraDataSource ( sp=> new Dictionary < string , string > ( )
119+ o . AddDefaultDataSource ( "ds0" ,
120+ "server=127.0.0.1;port=3306;database=dbdbd0;userid=root;password=root;" ) ;
121+ o . AddExtraDataSource ( sp => new Dictionary < string , string > ( )
95122 {
96- { "ds1" , "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" } ,
97- { "ds2" , "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;" }
123+ { "ds1" , "server=127.0.0.1;port=3306;database=dbdbd1;userid=root;password=root;" } ,
124+ { "ds2" , "server=127.0.0.1;port=3306;database=dbdbd2;userid=root;password=root;" }
98125 } ) ;
126+ o . AddReadWriteSeparation ( sp =>
127+ {
128+ return new Dictionary < string , IEnumerable < string > > ( )
129+ {
130+ {
131+ "ds0" ,
132+ new [ ]
133+ {
134+ "server=127.0.0.1;port=3306;database=dbdbd0_0;userid=root;password=root;"
135+ }
136+ }
137+ } ;
138+ } , defaultEnable : true , readStrategyEnum : ReadStrategyEnum . Loop ,
139+ readConnStringGetStrategy : ReadConnStringGetStrategyEnum . LatestEveryTime ) ;
99140 o . UseShardingMigrationConfigure ( b =>
100141 {
101142 b . ReplaceService < IMigrationsSqlGenerator , ShardingMySqlMigrationsSqlGenerator > ( ) ;
102143 } ) ;
103- } ) . ReplaceService < ITableEnsureManager , MySqlTableEnsureManager > ( ServiceLifetime . Singleton )
144+ } ) // .ReplaceService<ITableEnsureManager, MySqlTableEnsureManager>(ServiceLifetime.Singleton)
104145 . Build ( sp ) ;
105146 stopwatch . Stop ( ) ;
106- Console . WriteLine ( "ShardingRuntimeContext build:" + stopwatch . ElapsedMilliseconds ) ;
147+ Console . WriteLine ( "ShardingRuntimeContext build:" + stopwatch . ElapsedMilliseconds ) ;
107148 return shardingRuntimeContext ;
108149 } ) ;
109- services . AddDbContext < DefaultShardingDbContext > ( ShardingCoreExtension . UseDefaultSharding < DefaultShardingDbContext > ) ;
150+ services . AddDbContext < DefaultShardingDbContext > ( ShardingCoreExtension
151+ . UseMutliDefaultSharding < DefaultShardingDbContext > ) ;
110152 // services.AddShardingDbContext<DefaultShardingDbContext>()
111153 // .AddEntityConfig(o =>
112154 // {
@@ -151,29 +193,56 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
151193 {
152194 app . UseDeveloperExceptionPage ( ) ;
153195 }
154- app . ApplicationServices . UseAutoShardingCreate ( ) ;
155- var shardingRuntimeContext = app . ApplicationServices . GetRequiredService < IShardingRuntimeContext > ( ) ;
156- var entityMetadataManager = shardingRuntimeContext . GetEntityMetadataManager ( ) ;
157- var entityMetadata = entityMetadataManager . TryGet < SysUserMod > ( ) ;
158- using ( var scope = app . ApplicationServices . CreateScope ( ) )
196+
197+ var shardingRuntimeContextManager = app . ApplicationServices . GetRequiredService < IShardingRuntimeContextManager > ( ) ;
198+ var shardingRuntimeContexts = shardingRuntimeContextManager . GetAll ( ) ;
199+ foreach ( var keyValuePair in shardingRuntimeContexts )
159200 {
160- var defaultShardingDbContext = scope . ServiceProvider . GetService < DefaultShardingDbContext > ( ) ;
161- // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
162- {
163- defaultShardingDbContext . Database . Migrate ( ) ;
164- }
201+ keyValuePair . Value . UseAutoShardingCreate ( ) ;
202+ keyValuePair . Value . UseAutoTryCompensateTable ( ) ;
165203 }
204+ // app.ApplicationServices.UseAutoShardingCreate();
205+ // var shardingRuntimeContext = app.ApplicationServices.GetRequiredService<IShardingRuntimeContext>();
206+ // var entityMetadataManager = shardingRuntimeContext.GetEntityMetadataManager();
207+ // var entityMetadata = entityMetadataManager.TryGet<SysUserMod>();
208+ // using (var scope = app.ApplicationServices.CreateScope())
209+ // {
210+ // var defaultShardingDbContext = scope.ServiceProvider.GetService<DefaultShardingDbContext>();
211+ // // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
212+ // {
213+ // try
214+ // {
215+ //
216+ // defaultShardingDbContext.Database.Migrate();
217+ // }
218+ // catch (Exception e)
219+ // {
220+ // }
221+ // }
222+ // }
223+ // using (var scope = app.ApplicationServices.CreateScope())
224+ // {
225+ // var defaultShardingDbContext = scope.ServiceProvider.GetService<OtherDbContext>();
226+ // // if (defaultShardingDbContext.Database.GetPendingMigrations().Any())
227+ // {
228+ // try
229+ // {
230+ //
231+ // defaultShardingDbContext.Database.Migrate();
232+ // }
233+ // catch (Exception e)
234+ // {
235+ // }
236+ // }
237+ // }
238+ //
239+ // app.ApplicationServices.UseAutoTryCompensateTable(12);
166240
167- app . ApplicationServices . UseAutoTryCompensateTable ( 12 ) ;
168-
169241 app . UseRouting ( ) ;
170242
171243 app . UseAuthorization ( ) ;
172244
173- app . UseEndpoints ( endpoints =>
174- {
175- endpoints . MapControllers ( ) ;
176- } ) ;
245+ app . UseEndpoints ( endpoints => { endpoints . MapControllers ( ) ; } ) ;
177246 // for (int i = 1; i < 500; i++)
178247 // {
179248 // using (var conn = new MySqlConnection(
@@ -187,4 +256,4 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
187256 app . DbSeed ( ) ;
188257 }
189258 }
190- }
259+ }
0 commit comments