@@ -174,21 +174,36 @@ public virtual async Task<TEntity> CreateAsync(TEntity entity)
174174 /// </summary>
175175 private void LoadInverseRelationships ( object trackedRelationshipValue , RelationshipAttribute relationshipAttr )
176176 {
177- if ( relationshipAttr . InverseNavigation == null ) return ;
177+ if ( relationshipAttr . InverseNavigation == null || trackedRelationshipValue == null ) return ;
178178 if ( relationshipAttr is HasOneAttribute hasOneAttr )
179179 {
180- _context . Entry ( ( IIdentifiable ) trackedRelationshipValue ) . Reference ( hasOneAttr . InverseNavigation ) . Load ( ) ;
180+ var relationEntry = _context . Entry ( ( IIdentifiable ) trackedRelationshipValue ) ;
181+ if ( IsHasOneRelationship ( hasOneAttr . InverseNavigation , trackedRelationshipValue . GetType ( ) ) )
182+ {
183+ relationEntry . Reference ( hasOneAttr . InverseNavigation ) . Load ( ) ;
184+ } else
185+ {
186+ relationEntry . Collection ( hasOneAttr . InverseNavigation ) . Load ( ) ;
187+ }
181188 }
182- else if ( relationshipAttr is HasManyAttribute hasManyAttr )
189+ else if ( relationshipAttr is HasManyAttribute hasManyAttr && ! ( relationshipAttr is HasManyThroughAttribute ) )
183190 {
184191 foreach ( IIdentifiable relationshipValue in ( IList ) trackedRelationshipValue )
185192 {
186- _context . Entry ( ( IIdentifiable ) trackedRelationshipValue ) . Reference ( hasManyAttr . InverseNavigation ) . Load ( ) ;
193+ _context . Entry ( relationshipValue ) . Reference ( hasManyAttr . InverseNavigation ) . Load ( ) ;
187194 }
188195 }
189196 }
190197
191198
199+ private bool IsHasOneRelationship ( string internalRelationshipName , Type type )
200+ {
201+ var relationshipAttr = _jsonApiContext . ResourceGraph . GetContextEntity ( type ) . Relationships . Single ( r => r . InternalRelationshipName == internalRelationshipName ) ;
202+ if ( relationshipAttr is HasOneAttribute ) return true ;
203+ return false ;
204+ }
205+
206+
192207 /// <inheritdoc />
193208 public void DetachRelationshipPointers ( TEntity entity )
194209 {
@@ -223,14 +238,16 @@ public void DetachRelationshipPointers(TEntity entity)
223238 }
224239 }
225240
226- /// <inheritdoc />
241+ [ Obsolete ( "Use overload UpdateAsync(TEntity updatedEntity): providing parameter ID does no longer add anything relevant" ) ]
227242 public virtual async Task < TEntity > UpdateAsync ( TId id , TEntity updatedEntity )
228243 {
229- /// WHY is parameter "entity" even passed along to this method??
230- /// It does nothing!
231-
232- var oldEntity = await GetAsync ( id ) ;
244+ return await UpdateAsync ( updatedEntity ) ;
245+ }
233246
247+ /// <inheritdoc />
248+ public virtual async Task < TEntity > UpdateAsync ( TEntity updatedEntity )
249+ {
250+ var oldEntity = await GetAsync ( updatedEntity . Id ) ;
234251 if ( oldEntity == null )
235252 return null ;
236253
@@ -244,6 +261,7 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity updatedEntity)
244261 LoadInverseRelationships ( trackedRelationshipValue , relationshipAttr ) ;
245262 AssignRelationshipValue ( oldEntity , trackedRelationshipValue , relationshipAttr ) ;
246263 }
264+
247265 await _context . SaveChangesAsync ( ) ;
248266 return oldEntity ;
249267 }
@@ -375,7 +393,9 @@ public virtual async Task<IEnumerable<TEntity>> PageAsync(IQueryable<TEntity> en
375393 {
376394 if ( pageNumber >= 0 )
377395 {
378- return await entities . PageForward ( pageSize , pageNumber ) . ToListAsync ( ) ;
396+ // the IQueryable returned from the hook executor is sometimes consumed here.
397+ // In this case, it does not support .ToListAsync(), so we use the method below.
398+ return await this . ToListAsync ( entities . PageForward ( pageSize , pageNumber ) ) ;
379399 }
380400
381401 // since EntityFramework does not support IQueryable.Reverse(), we need to know the number of queried entities
@@ -384,11 +404,10 @@ public virtual async Task<IEnumerable<TEntity>> PageAsync(IQueryable<TEntity> en
384404 // may be negative
385405 int virtualFirstIndex = numberOfEntities - pageSize * Math . Abs ( pageNumber ) ;
386406 int numberOfElementsInPage = Math . Min ( pageSize , virtualFirstIndex + pageSize ) ;
387-
388- return await entities
407+
408+ return await ToListAsync ( entities
389409 . Skip ( virtualFirstIndex )
390- . Take ( numberOfElementsInPage )
391- . ToListAsync ( ) ;
410+ . Take ( numberOfElementsInPage ) ) ;
392411 }
393412
394413 /// <inheritdoc />
@@ -441,7 +460,7 @@ protected void LoadCurrentRelationships(TEntity oldEntity, RelationshipAttribute
441460 /// retrieve from the context WHICH relationships to update, but the actual
442461 /// values should not come from the context.
443462 /// </summary>
444- protected void AssignRelationshipValue ( TEntity oldEntity , object relationshipValue , RelationshipAttribute relationshipAttribute )
463+ private void AssignRelationshipValue ( TEntity oldEntity , object relationshipValue , RelationshipAttribute relationshipAttribute )
445464 {
446465 if ( relationshipAttribute is HasManyThroughAttribute throughAttribute )
447466 {
@@ -479,7 +498,7 @@ private void AssignHasManyThrough(TEntity entity, HasManyThroughAttribute hasMan
479498 /// A helper method that gets the relationship value in the case of
480499 /// entity resource separation.
481500 /// </summary>
482- IIdentifiable GetEntityResourceSeparationValue ( TEntity entity , HasOneAttribute attribute )
501+ private IIdentifiable GetEntityResourceSeparationValue ( TEntity entity , HasOneAttribute attribute )
483502 {
484503 if ( attribute . EntityPropertyName == null )
485504 {
@@ -492,7 +511,7 @@ IIdentifiable GetEntityResourceSeparationValue(TEntity entity, HasOneAttribute a
492511 /// A helper method that gets the relationship value in the case of
493512 /// entity resource separation.
494513 /// </summary>
495- IEnumerable < IIdentifiable > GetEntityResourceSeparationValue ( TEntity entity , HasManyAttribute attribute )
514+ private IEnumerable < IIdentifiable > GetEntityResourceSeparationValue ( TEntity entity , HasManyAttribute attribute )
496515 {
497516 if ( attribute . EntityPropertyName == null )
498517 {
@@ -508,7 +527,7 @@ IEnumerable<IIdentifiable> GetEntityResourceSeparationValue(TEntity entity, HasM
508527 ///
509528 /// useful article: https://stackoverflow.com/questions/30987806/dbset-attachentity-vs-dbcontext-entryentity-state-entitystate-modified
510529 /// </summary>
511- IIdentifiable AttachOrGetTracked ( IIdentifiable relationshipValue )
530+ private IIdentifiable AttachOrGetTracked ( IIdentifiable relationshipValue )
512531 {
513532 var trackedEntity = _context . GetTrackedEntity ( relationshipValue ) ;
514533
0 commit comments