1919using ShardingCore . Core . VirtualRoutes . TableRoutes . RouteTails . Abstractions ;
2020using ShardingCore . Core . VirtualTables ;
2121using ShardingCore . Extensions ;
22+ using ShardingCore . Jobs ;
23+ using ShardingCore . Jobs . Abstaractions ;
2224using ShardingCore . Sharding . Abstractions ;
2325using ShardingCore . TableCreator ;
2426using ShardingCore . Utils ;
@@ -63,6 +65,7 @@ public void Init()
6365 // virtualDataSource.AddPhysicDataSource(new DefaultPhysicDataSource(dataSourceKv.Key,
6466 // dataSourceKv.Value, false));
6567 //}
68+ ISet < Type > entitiesInitSet = new HashSet < Type > ( ) ;
6669 foreach ( var dataSourceKv in dataSources )
6770 {
6871
@@ -78,41 +81,121 @@ public void Init()
7881 foreach ( var entity in context . Model . GetEntityTypes ( ) )
7982 {
8083 var entityType = entity . ClrType ;
84+
8185 if ( _shardingConfigOption . HasVirtualDataSourceRoute ( entityType ) ||
82- _shardingConfigOption . HasVirtualTableRoute ( entityType ) )
86+ _shardingConfigOption . HasVirtualTableRoute ( entityType ) )
8387 {
84- //获取ShardingEntity的实际表名
88+ //只初始化一次
89+ if ( ! entitiesInitSet . Contains ( entityType ) )
90+ {
91+ //获取ShardingEntity的实际表名
8592#if ! EFCORE2
86- var virtualTableName = context . Model . FindEntityType ( entityType ) . GetTableName ( ) ;
93+ var virtualTableName = context . Model . FindEntityType ( entityType ) . GetTableName ( ) ;
8794#endif
8895#if EFCORE2
89- var virtualTableName = context . Model . FindEntityType ( entityType ) . Relational ( ) . TableName ;
96+ var virtualTableName = context . Model . FindEntityType ( entityType ) . Relational ( ) . TableName ;
9097#endif
91- var entityMetadataInitializerType = typeof ( EntityMetadataInitializer < , > ) . GetGenericType1 ( typeof ( TShardingDbContext ) , entityType ) ;
92- var constructors
93- = entityMetadataInitializerType . GetTypeInfo ( ) . DeclaredConstructors
94- . Where ( c => ! c . IsStatic && c . IsPublic )
95- . ToArray ( ) ;
96- var @params = constructors [ 0 ] . GetParameters ( ) . Select ( ( o , i ) =>
97- {
98-
99-
100- if ( i == 0 )
98+ var entityMetadataInitializerType = typeof ( EntityMetadataInitializer < , > ) . GetGenericType1 ( typeof ( TShardingDbContext ) , entityType ) ;
99+ var constructors
100+ = entityMetadataInitializerType . GetTypeInfo ( ) . DeclaredConstructors
101+ . Where ( c => ! c . IsStatic && c . IsPublic )
102+ . ToArray ( ) ;
103+ var @params = constructors [ 0 ] . GetParameters ( ) . Select ( ( o , i ) =>
101104 {
102- if ( o . ParameterType != typeof ( EntityMetadataEnsureParams ) )
103- throw new InvalidOperationException ( $ "{ typeof ( EntityMetadataInitializer < , > ) . FullName } constructors first params type should { typeof ( EntityMetadataEnsureParams ) . FullName } ") ;
104- return new EntityMetadataEnsureParams ( dataSourceName , entity , virtualTableName ) ;
105- }
106-
107- return ShardingContainer . GetService ( o . ParameterType ) ;
108- } ) . ToArray ( ) ;
109- var entityMetadataInitializer = ( IEntityMetadataInitializer ) Activator . CreateInstance ( entityMetadataInitializerType , @params ) ;
110- entityMetadataInitializer . Initialize ( ) ;
105+
106+
107+ if ( i == 0 )
108+ {
109+ if ( o . ParameterType != typeof ( EntityMetadataEnsureParams ) )
110+ throw new InvalidOperationException ( $ "{ typeof ( EntityMetadataInitializer < , > ) . FullName } constructors first params type should { typeof ( EntityMetadataEnsureParams ) . FullName } ") ;
111+ return new EntityMetadataEnsureParams ( dataSourceName , entity , virtualTableName ) ;
112+ }
113+
114+ return ShardingContainer . GetService ( o . ParameterType ) ;
115+ } ) . ToArray ( ) ;
116+ var entityMetadataInitializer = ( IEntityMetadataInitializer ) Activator . CreateInstance ( entityMetadataInitializerType , @params ) ;
117+ entityMetadataInitializer . Initialize ( ) ;
118+ entitiesInitSet . Add ( entityType ) ;
119+ }
120+
121+ var virtualTable = _virtualTableManager . GetVirtualTable ( entityType ) ;
122+ //创建表
123+ CreateDataTable ( dataSourceName , virtualTable ) ;
124+ //var virtualTableRoute = virtualTable.GetVirtualRoute();
125+ ////添加任务
126+ //if (virtualTableRoute is IJob routeJob && routeJob.StartJob())
127+ //{
128+ // var jobManager = ShardingContainer.GetService<IJobManager>();
129+ // var jobEntries = JobTypeParser.Parse(virtualTableRoute.GetType());
130+ // jobEntries.ForEach(o =>
131+ // {
132+ // o.JobName = $"{routeJob.JobName}:{o.JobName}";
133+ // });
134+ // foreach (var jobEntry in jobEntries)
135+ // {
136+ // jobManager.AddJob(jobEntry);
137+ // }
138+ //}
139+ }
140+ }
141+ }
142+ }
143+ }
144+ private void CreateDataTable ( string dataSourceName , IVirtualTable virtualTable )
145+ {
146+ var entityMetadata = virtualTable . EntityMetadata ;
147+ foreach ( var tail in virtualTable . GetVirtualRoute ( ) . GetAllTails ( ) )
148+ {
149+ if ( NeedCreateTable ( entityMetadata ) )
150+ {
151+ try
152+ {
153+ //添加物理表
154+ virtualTable . AddPhysicTable ( new DefaultPhysicTable ( virtualTable , tail ) ) ;
155+ _tableCreator . CreateTable ( dataSourceName , entityMetadata . EntityType , tail ) ;
156+ }
157+ catch ( Exception e )
158+ {
159+ if ( ! _shardingConfigOption . IgnoreCreateTableError . GetValueOrDefault ( ) )
160+ {
161+ _logger . LogWarning (
162+ $ "table :{ virtualTable . GetVirtualTableName ( ) } { entityMetadata . TableSeparator } { tail } will created.", e ) ;
111163 }
112164 }
113165 }
166+ else
167+ {
168+ //添加物理表
169+ virtualTable . AddPhysicTable ( new DefaultPhysicTable ( virtualTable , tail ) ) ;
170+ }
171+
114172 }
115173 }
174+ private bool NeedCreateTable ( EntityMetadata entityMetadata )
175+ {
176+ if ( entityMetadata . AutoCreateTable . HasValue )
177+ {
178+ if ( entityMetadata . AutoCreateTable . Value )
179+ return entityMetadata . AutoCreateTable . Value ;
180+ else
181+ {
182+ if ( entityMetadata . AutoCreateDataSourceTable . HasValue )
183+ return entityMetadata . AutoCreateDataSourceTable . Value ;
184+ }
185+ }
186+ if ( entityMetadata . AutoCreateDataSourceTable . HasValue )
187+ {
188+ if ( entityMetadata . AutoCreateDataSourceTable . Value )
189+ return entityMetadata . AutoCreateDataSourceTable . Value ;
190+ else
191+ {
192+ if ( entityMetadata . AutoCreateTable . HasValue )
193+ return entityMetadata . AutoCreateTable . Value ;
194+ }
195+ }
196+
197+ return _shardingConfigOption . CreateShardingTableOnStart . GetValueOrDefault ( ) ;
198+ }
116199 private void EnsureCreated ( DbContext context , string dataSourceName )
117200 {
118201 if ( context is IShardingDbContext shardingDbContext )
@@ -122,7 +205,8 @@ private void EnsureCreated(DbContext context, string dataSourceName)
122205
123206 lock ( modelCacheSyncObject )
124207 {
125- dbContext . RemoveDbContextRelationModelThatIsShardingTable ( ) ;
208+ var shardingEntitiyTypes = _shardingConfigOption . GetShardingTableRouteTypes ( ) ;
209+ dbContext . RemoveDbContextRelationModelThatIsShardingTable ( shardingEntitiyTypes ) ;
126210 dbContext . Database . EnsureCreated ( ) ;
127211 dbContext . RemoveModelCache ( ) ;
128212 }
0 commit comments