@@ -212,16 +212,74 @@ public BulkDicEntry(DbContext innerDbContext, LinkedList<TEntity> innerEntities)
212212 public DbContext InnerDbContext { get ; }
213213 public LinkedList < TEntity > InnerEntities { get ; }
214214 }
215+
216+ public static Dictionary < DbContext , IEnumerable < TEntity > > BulkShardingTableEnumerable < TShardingDbContext , TEntity > ( this TShardingDbContext shardingDbContext ,
217+ IEnumerable < TEntity > entities ) where TShardingDbContext : DbContext , IShardingDbContext
218+ where TEntity : class
219+ {
220+
221+ if ( typeof ( TEntity ) . IsShardingDataSource ( ) )
222+ throw new InvalidOperationException ( typeof ( TEntity ) . FullName ) ;
223+ return shardingDbContext . BulkShardingEnumerable ( entities ) . First ( ) . Value ;
224+ }
215225 /// <summary>
216226 /// 根据条件表达式解析
217227 /// </summary>
228+ /// <typeparam name="TShardingDbContext"></typeparam>
218229 /// <typeparam name="TEntity"></typeparam>
219230 /// <param name="shardingDbContext"></param>
220231 /// <param name="where"></param>
221232 /// <returns></returns>
222- public static IEnumerable < DbContext > BulkShardingExpression < TEntity > ( this IShardingDbContext shardingDbContext , Expression < Func < TEntity , bool > > where ) where TEntity : class
233+ public static IDictionary < string , IEnumerable < DbContext > > BulkShardingExpression < TShardingDbContext , TEntity > ( this TShardingDbContext shardingDbContext , Expression < Func < TEntity , bool > > where ) where TEntity : class
234+ where TShardingDbContext : DbContext , IShardingDbContext
235+ {
236+
237+ var virtualDataSource = ShardingContainer . GetService < IVirtualDataSource < TShardingDbContext > > ( ) ;
238+ var routeTailFactory = ShardingContainer . GetService < IRouteTailFactory > ( ) ;
239+ var virtualTableManager = ShardingContainer . GetService < IVirtualTableManager < TShardingDbContext > > ( ) ;
240+
241+ var dataSourceNames = virtualDataSource . GetDataSourceNames ( where ) ;
242+ var result = new Dictionary < string , LinkedList < DbContext > > ( ) ;
243+ var entityType = typeof ( TEntity ) ;
244+
245+ foreach ( var dataSourceName in dataSourceNames )
246+ {
247+ if ( ! result . TryGetValue ( dataSourceName , out var dbContexts ) )
248+ {
249+ dbContexts = new LinkedList < DbContext > ( ) ;
250+ result . Add ( dataSourceName , dbContexts ) ;
251+ }
252+ if ( entityType . IsShardingTable ( ) )
253+ {
254+ var physicTables = virtualTableManager . GetVirtualTable ( entityType ) . RouteTo ( new ShardingTableRouteConfig ( predicate : @where ) ) ;
255+ if ( physicTables . IsEmpty ( ) )
256+ throw new ShardingCoreException ( $ "{ where . ShardingPrint ( ) } cant found ant physic table") ;
257+
258+ var dbs = physicTables . Select ( o => shardingDbContext . GetDbContext ( dataSourceName , true , routeTailFactory . Create ( o . Tail ) ) ) . ToList ( ) ;
259+ foreach ( var dbContext in dbs )
260+ {
261+ dbContexts . AddLast ( dbContext ) ;
262+ }
263+ }
264+ else
265+ {
266+ var dbContext = shardingDbContext . GetDbContext ( dataSourceName , true , routeTailFactory . Create ( string . Empty ) ) ;
267+ dbContexts . AddLast ( dbContext ) ;
268+ }
269+
270+ }
271+
272+ return result . ToDictionary ( o=> o . Key , o=> ( IEnumerable < DbContext > ) o . Value ) ;
273+ }
274+
275+ public static IEnumerable < DbContext > BulkShardingTableExpression < TShardingDbContext , TEntity > ( this TShardingDbContext shardingDbContext , Expression < Func < TEntity , bool > > where ) where TEntity : class
276+ where TShardingDbContext : DbContext , IShardingDbContext
223277 {
224- return shardingDbContext . CreateExpressionDbContext ( where ) ;
278+
279+ if ( typeof ( TEntity ) . IsShardingDataSource ( ) )
280+ throw new InvalidOperationException ( typeof ( TEntity ) . FullName ) ;
281+ return shardingDbContext . BulkShardingExpression < TShardingDbContext , TEntity > ( where ) . First ( ) . Value ;
225282 }
283+
226284 }
227285}
0 commit comments