|
| 1 | +using System; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using ShardingCore; |
| 5 | +using ShardingCore.Core.EntityMetadatas; |
| 6 | +using ShardingCore.Core.PhysicTables; |
| 7 | +using ShardingCore.Core.VirtualDatabase.VirtualDataSources; |
| 8 | +using ShardingCore.Core.VirtualDatabase.VirtualDataSources.PhysicDataSources; |
| 9 | +using ShardingCore.Core.VirtualDatabase.VirtualTables; |
| 10 | +using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions; |
| 11 | +using ShardingCore.Core.VirtualTables; |
| 12 | +using ShardingCore.Exceptions; |
| 13 | +using ShardingCore.Extensions; |
| 14 | +using ShardingCore.Sharding.Abstractions; |
| 15 | +using ShardingCore.TableCreator; |
| 16 | + |
| 17 | +namespace Sample.SqlServerShardingAll |
| 18 | +{ |
| 19 | + public class DbContextHelper |
| 20 | + { |
| 21 | + public static void EnsureSubDbCreatedAndCreateSubTables(string dataSourceName, string connectionString, Type entityType, int sum4SubTable) |
| 22 | + { |
| 23 | + var _entityMetadataManager = ShardingContainer.GetService<IEntityMetadataManager<MyDbContext>>(); |
| 24 | + var _virtualDataSource = ShardingContainer.GetService<IVirtualDataSource<MyDbContext>>(); |
| 25 | + var _virtualTableManager = ShardingContainer.GetService<IVirtualTableManager<MyDbContext>>(); |
| 26 | + var _tableCreator = ShardingContainer.GetService<IShardingTableCreator<MyDbContext>>(); |
| 27 | + |
| 28 | + using (var serviceScope = ShardingContainer.ServiceProvider.CreateScope()) |
| 29 | + { |
| 30 | + _virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource(dataSourceName, connectionString, false)); |
| 31 | + var virtualDataSourceRoute = _virtualDataSource.GetRoute(entityType); |
| 32 | + virtualDataSourceRoute.AddDataSourceName(dataSourceName); |
| 33 | + |
| 34 | + using var context = (DbContext)serviceScope.ServiceProvider.GetService(typeof(MyDbContext)); |
| 35 | + EnsureCreated(context, dataSourceName); |
| 36 | + foreach (var entity in context.Model.GetEntityTypes()) |
| 37 | + { |
| 38 | + if (entity.ClrType != entityType) |
| 39 | + { |
| 40 | + continue; |
| 41 | + } |
| 42 | + if (_entityMetadataManager.IsShardingTable(entityType)) |
| 43 | + { |
| 44 | + var virtualTable = _virtualTableManager.GetVirtualTable(entityType); |
| 45 | + //创建表 |
| 46 | + CreateDataTable(dataSourceName, virtualTable, sum4SubTable); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + _tableCreator.CreateTable(dataSourceName, entityType, string.Empty); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + private static void CreateDataTable(string dataSourceName, IVirtualTable virtualTable, int sum4SubTable) |
| 56 | + { |
| 57 | + var _tableCreator = ShardingContainer.GetService<IShardingTableCreator<MyDbContext>>(); |
| 58 | + var entityMetadata = virtualTable.EntityMetadata; |
| 59 | + int currentCount = 0; |
| 60 | + foreach (var tail in virtualTable.GetVirtualRoute().GetAllTails()) |
| 61 | + { |
| 62 | + if (currentCount >= sum4SubTable) |
| 63 | + { |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + if (NeedCreateTable(entityMetadata)) |
| 68 | + { |
| 69 | + try |
| 70 | + { |
| 71 | + //添加物理表 |
| 72 | + virtualTable.AddPhysicTable(new DefaultPhysicTable(virtualTable, tail)); |
| 73 | + _tableCreator.CreateTable(dataSourceName, entityMetadata.EntityType, tail); |
| 74 | + } |
| 75 | + catch (Exception ex) |
| 76 | + { |
| 77 | + //if (!_shardingConfigOption.IgnoreCreateTableError.GetValueOrDefault()) |
| 78 | + //{ |
| 79 | + // _logger.LogWarning(ex, |
| 80 | + // $"table :{virtualTable.GetVirtualTableName()}{entityMetadata.TableSeparator}{tail} will created."); |
| 81 | + //} |
| 82 | + //TODO: 记录异常日志 |
| 83 | + System.Diagnostics.Trace.TraceError($"DbContextHelper-->CreateDataTable ERROR: {ex}"); |
| 84 | + } |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + //添加物理表 |
| 89 | + virtualTable.AddPhysicTable(new DefaultPhysicTable(virtualTable, tail)); |
| 90 | + } |
| 91 | + currentCount++; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + private static bool NeedCreateTable(EntityMetadata entityMetadata) |
| 97 | + { |
| 98 | + if (entityMetadata.AutoCreateTable.HasValue) |
| 99 | + { |
| 100 | + if (entityMetadata.AutoCreateTable.Value) |
| 101 | + return entityMetadata.AutoCreateTable.Value; |
| 102 | + else |
| 103 | + { |
| 104 | + if (entityMetadata.AutoCreateDataSourceTable.HasValue) |
| 105 | + return entityMetadata.AutoCreateDataSourceTable.Value; |
| 106 | + } |
| 107 | + } |
| 108 | + if (entityMetadata.AutoCreateDataSourceTable.HasValue) |
| 109 | + { |
| 110 | + if (entityMetadata.AutoCreateDataSourceTable.Value) |
| 111 | + return entityMetadata.AutoCreateDataSourceTable.Value; |
| 112 | + else |
| 113 | + { |
| 114 | + if (entityMetadata.AutoCreateTable.HasValue) |
| 115 | + return entityMetadata.AutoCreateTable.Value; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + //return _shardingConfigOption.CreateShardingTableOnStart.GetValueOrDefault(); |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + private static void EnsureCreated(DbContext context, string dataSourceName) |
| 124 | + { |
| 125 | + var _routeTailFactory = ShardingContainer.GetService<IRouteTailFactory>(); |
| 126 | + |
| 127 | + if (context is IShardingDbContext shardingDbContext) |
| 128 | + { |
| 129 | + var dbContext = shardingDbContext.GetDbContext(dataSourceName, false, _routeTailFactory.Create(string.Empty)); |
| 130 | + |
| 131 | + var modelCacheSyncObject = dbContext.GetModelCacheSyncObject(); |
| 132 | + |
| 133 | + var acquire = System.Threading.Monitor.TryEnter(modelCacheSyncObject, TimeSpan.FromSeconds(3)); |
| 134 | + if (!acquire) |
| 135 | + { |
| 136 | + throw new ShardingCoreException("cant get modelCacheSyncObject lock"); |
| 137 | + } |
| 138 | + |
| 139 | + try |
| 140 | + { |
| 141 | + dbContext.RemoveDbContextRelationModelThatIsShardingTable(); |
| 142 | + dbContext.Database.EnsureCreated(); |
| 143 | + dbContext.RemoveModelCache(); |
| 144 | + } |
| 145 | + finally |
| 146 | + { |
| 147 | + System.Threading.Monitor.Exit(modelCacheSyncObject); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments