1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using Microsoft . EntityFrameworkCore ;
5+ using Microsoft . EntityFrameworkCore . ChangeTracking ;
6+ using Microsoft . EntityFrameworkCore . ChangeTracking . Internal ;
7+ using Microsoft . EntityFrameworkCore . Metadata ;
8+ using ShardingCore . Sharding . Abstractions ;
9+
10+ namespace ShardingCore . EFCores . ChangeTrackers
11+ {
12+ public class ShardingChangeTracker : ChangeTracker
13+ {
14+ private readonly DbContext _dbContext ;
15+
16+ public ShardingChangeTracker ( DbContext context , IStateManager stateManager , IChangeDetector changeDetector ,
17+ IModel model , IEntityEntryGraphIterator graphIterator ) : base ( context , stateManager , changeDetector , model ,
18+ graphIterator )
19+ {
20+ _dbContext = context ;
21+ }
22+ #if ! EFCORE2 && ! EFCORE3 && ! EFCORE5 && ! EFCORE6
23+ 1
24+ #endif
25+
26+ public override bool HasChanges ( )
27+ {
28+ if ( _dbContext is ICurrentDbContextDiscover currentDbContextDiscover )
29+ {
30+ return currentDbContextDiscover . GetCurrentDbContexts ( ) . Any ( o =>
31+ o . Value . GetCurrentContexts ( ) . Any ( r => r . Value . ChangeTracker . HasChanges ( ) ) ) ;
32+ }
33+
34+ return base . HasChanges ( ) ;
35+ }
36+
37+ public override IEnumerable < EntityEntry > Entries ( )
38+ {
39+ if ( _dbContext is ICurrentDbContextDiscover currentDbContextDiscover )
40+ {
41+ return currentDbContextDiscover . GetCurrentDbContexts ( ) . SelectMany ( o =>
42+ o . Value . GetCurrentContexts ( ) . SelectMany ( cd => cd . Value . ChangeTracker . Entries ( ) ) ) ;
43+ }
44+
45+ return base . Entries ( ) ;
46+ }
47+
48+ public override IEnumerable < EntityEntry < TEntity > > Entries < TEntity > ( )
49+ {
50+ if ( _dbContext is ICurrentDbContextDiscover currentDbContextDiscover )
51+ {
52+ return currentDbContextDiscover . GetCurrentDbContexts ( ) . SelectMany ( o =>
53+ o . Value . GetCurrentContexts ( ) . SelectMany ( cd => cd . Value . ChangeTracker . Entries < TEntity > ( ) ) ) ;
54+ }
55+
56+ return base . Entries < TEntity > ( ) ;
57+ }
58+
59+ public override void DetectChanges ( )
60+ {
61+ if ( _dbContext is ICurrentDbContextDiscover )
62+ {
63+ Do ( c => c . DetectChanges ( ) ) ;
64+ return ;
65+ }
66+ base . DetectChanges ( ) ;
67+ }
68+
69+ public override void AcceptAllChanges ( )
70+ {
71+ if ( _dbContext is ICurrentDbContextDiscover )
72+ {
73+ Do ( c => c . AcceptAllChanges ( ) ) ;
74+ return ;
75+ }
76+ base . AcceptAllChanges ( ) ;
77+ }
78+
79+ private void Do ( Action < ChangeTracker > action )
80+ {
81+ var dataSourceDbContexts = ( ( ICurrentDbContextDiscover ) _dbContext ) . GetCurrentDbContexts ( ) ;
82+ foreach ( var dataSourceDbContext in dataSourceDbContexts )
83+ {
84+ var currentContexts = dataSourceDbContext . Value . GetCurrentContexts ( ) ;
85+ foreach ( var keyValuePair in currentContexts )
86+ {
87+ action ( keyValuePair . Value . ChangeTracker ) ;
88+ }
89+ }
90+ }
91+
92+ public override void TrackGraph ( object rootEntity , Action < EntityEntryGraphNode > callback )
93+ {
94+ if ( _dbContext is ICurrentDbContextDiscover )
95+ {
96+ Do ( c => c . TrackGraph ( rootEntity , callback ) ) ;
97+ return ;
98+ }
99+ base . TrackGraph ( rootEntity , callback ) ;
100+ }
101+
102+ #if ! EFCORE2
103+ public override void TrackGraph < TState > ( object rootEntity , TState state , Func < EntityEntryGraphNode < TState > , bool > callback ) where TState : default
104+ {
105+ if ( _dbContext is ICurrentDbContextDiscover )
106+ {
107+ Do ( c => c . TrackGraph ( rootEntity , state , callback ) ) ;
108+ return ;
109+ }
110+ base . TrackGraph ( rootEntity , state , callback ) ;
111+ }
112+
113+ public override void CascadeChanges ( )
114+ {
115+ if ( _dbContext is ICurrentDbContextDiscover )
116+ {
117+ Do ( c => c . CascadeChanges ( ) ) ;
118+ return ;
119+ }
120+ base . CascadeChanges ( ) ;
121+ }
122+
123+ #endif
124+ #if ! EFCORE2 && ! EFCORE3
125+ public override void Clear ( )
126+ {
127+ if ( _dbContext is ICurrentDbContextDiscover )
128+ {
129+ Do ( c => c . Clear ( ) ) ;
130+ return ;
131+ }
132+ base . Clear ( ) ;
133+ }
134+ #endif
135+
136+ #if EFCORE2
137+ public override void TrackGraph < TState > ( object rootEntity , TState state , Func < EntityEntryGraphNode , TState , bool > callback )
138+ {
139+ if ( _dbContext is ICurrentDbContextDiscover )
140+ {
141+ Do ( c => c . TrackGraph ( rootEntity , state , callback ) ) ;
142+ return ;
143+ }
144+ base . TrackGraph ( rootEntity , state , callback ) ;
145+ }
146+ #endif
147+ }
148+ }
0 commit comments