@@ -54,6 +54,8 @@ public Identifier(EntityState entityState, AssociationInfo association)
5454 private readonly IDictionary < Identifier , HashSet < EntityState > > addedReferences = new Dictionary < Identifier , HashSet < EntityState > > ( ) ;
5555 private readonly object accessGuard = new ( ) ;
5656
57+ private bool changesDisabled ;
58+
5759 public int RemovedReferencesCount => removedReferences . Values . Sum ( el => el . Count ) ;
5860
5961 public int AddedReferencesCount => addedReferences . Values . Sum ( el => el . Count ) ;
@@ -98,6 +100,12 @@ public void Clear()
98100 }
99101 }
100102
103+ internal Core . Disposable PreventChanges ( )
104+ {
105+ changesDisabled = true ;
106+ return new Core . Disposable ( ( a ) => changesDisabled = false ) ;
107+ }
108+
101109 private void RegisterChange ( EntityState referencedState , EntityState referencingState , EntityState noLongerReferencedState , AssociationInfo association )
102110 {
103111 ArgumentValidator . EnsureArgumentNotNull ( association , "association" ) ;
@@ -129,7 +137,7 @@ private void RegisterChange(EntityState referencedState, EntityState referencing
129137 private void RegisterRemoveInternal ( Identifier oldKey , EntityState referencingState )
130138 {
131139 if ( addedReferences . TryGetValue ( oldKey , out var addedRefs ) ) {
132- EnsureChangesAreNotPersisting ( ) ;
140+ EnsureRegistrationsAllowed ( ) ;
133141 if ( addedRefs . Remove ( referencingState ) ) {
134142 if ( addedRefs . Count == 0 ) {
135143 _ = addedReferences . Remove ( oldKey ) ;
@@ -143,14 +151,14 @@ private void RegisterRemoveInternal(Identifier oldKey, EntityState referencingSt
143151 }
144152 return ;
145153 }
146- EnsureChangesAreNotPersisting ( ) ;
154+ EnsureRegistrationsAllowed ( ) ;
147155 removedReferences . Add ( oldKey , new HashSet < EntityState > { referencingState } ) ;
148156 }
149157
150158 private void RegisterAddInternal ( Identifier newKey , EntityState referencingState )
151159 {
152160 if ( removedReferences . TryGetValue ( newKey , out var removedRefs ) ) {
153- EnsureChangesAreNotPersisting ( ) ;
161+ EnsureRegistrationsAllowed ( ) ;
154162 if ( removedRefs . Remove ( referencingState ) ) {
155163 if ( removedRefs . Count == 0 ) {
156164 _ = removedReferences . Remove ( newKey ) ;
@@ -165,7 +173,7 @@ private void RegisterAddInternal(Identifier newKey, EntityState referencingState
165173 }
166174 return ;
167175 }
168- EnsureChangesAreNotPersisting ( ) ;
176+ EnsureRegistrationsAllowed ( ) ;
169177 addedReferences . Add ( newKey , new HashSet < EntityState > { referencingState } ) ;
170178 }
171179
@@ -276,6 +284,14 @@ private EntityState GetStructureFieldValue(FieldInfo fieldOfStructure, Structure
276284 private string BuildNameOfEntityField ( FieldInfo fieldOfOwner , FieldInfo referenceFieldOfStructure ) =>
277285 $ "{ fieldOfOwner . Name } .{ referenceFieldOfStructure . Name } ";
278286
287+ private void EnsureRegistrationsAllowed ( )
288+ {
289+ if ( changesDisabled ) {
290+ throw new InvalidOperationException (
291+ string . Format ( Strings . ExSessionXIsActivelyPersistingChangesNoPersistentChangesAllowed , Session . Guid ) ) ;
292+ }
293+ }
294+
279295 internal NonPairedReferenceChangesRegistry ( Session session )
280296 : base ( session )
281297 {
0 commit comments