@@ -163,7 +163,7 @@ public void DetachRelationshipPointers(TEntity entity)
163163 {
164164 foreach ( var hasOneRelationship in _jsonApiContext . HasOneRelationshipPointers . Get ( ) )
165165 {
166- var hasOne = ( HasOneAttribute ) hasOneRelationship . Key ;
166+ var hasOne = ( HasOneAttribute ) hasOneRelationship . Key ;
167167 if ( hasOne . EntityPropertyName != null )
168168 {
169169 var relatedEntity = entity . GetType ( ) . GetProperty ( hasOne . EntityPropertyName ) ? . GetValue ( entity ) ;
@@ -178,7 +178,7 @@ public void DetachRelationshipPointers(TEntity entity)
178178
179179 foreach ( var hasManyRelationship in _jsonApiContext . HasManyRelationshipPointers . Get ( ) )
180180 {
181- var hasMany = ( HasManyAttribute ) hasManyRelationship . Key ;
181+ var hasMany = ( HasManyAttribute ) hasManyRelationship . Key ;
182182 if ( hasMany . EntityPropertyName != null )
183183 {
184184 var relatedList = ( IList ) entity . GetType ( ) . GetProperty ( hasMany . EntityPropertyName ) ? . GetValue ( entity ) ;
@@ -194,7 +194,7 @@ public void DetachRelationshipPointers(TEntity entity)
194194 _context . Entry ( pointer ) . State = EntityState . Detached ;
195195 }
196196 }
197-
197+
198198 // HACK: detaching has many relationships doesn't appear to be sufficient
199199 // the navigation property actually needs to be nulled out, otherwise
200200 // EF adds duplicate instances to the collection
@@ -234,7 +234,7 @@ private void AttachHasMany(TEntity entity, HasManyAttribute relationship, IList
234234 {
235235 _context . Entry ( pointer ) . State = EntityState . Unchanged ;
236236 }
237- }
237+ }
238238 }
239239
240240 private void AttachHasManyThrough ( TEntity entity , HasManyThroughAttribute hasManyThrough , IList pointers )
@@ -270,7 +270,7 @@ private void AttachHasOnePointers(TEntity entity)
270270 if ( relationship . Key . GetType ( ) != typeof ( HasOneAttribute ) )
271271 continue ;
272272
273- var hasOne = ( HasOneAttribute ) relationship . Key ;
273+ var hasOne = ( HasOneAttribute ) relationship . Key ;
274274 if ( hasOne . EntityPropertyName != null )
275275 {
276276 var relatedEntity = entity . GetType ( ) . GetProperty ( hasOne . EntityPropertyName ) ? . GetValue ( entity ) ;
@@ -296,13 +296,19 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
296296 foreach ( var attr in _jsonApiContext . AttributesToUpdate )
297297 attr . Key . SetValue ( oldEntity , attr . Value ) ;
298298
299- foreach ( var relationship in _jsonApiContext . RelationshipsToUpdate )
300- relationship . Key . SetValue ( oldEntity , relationship . Value ) ;
301-
302- AttachRelationships ( oldEntity ) ;
303-
299+ if ( _jsonApiContext . RelationshipsToUpdate . Any ( ) )
300+ {
301+ AttachRelationships ( oldEntity ) ;
302+ foreach ( var relationship in _jsonApiContext . RelationshipsToUpdate )
303+ {
304+ /// If we are updating to-many relations from PATCH, we need to include the relation first,
305+ /// else it will not peform a complete replacement, as required by the specs.
306+ if ( relationship . Key . IsHasMany )
307+ await _context . Entry ( oldEntity ) . Collection ( relationship . Key . InternalRelationshipName ) . LoadAsync ( ) ;
308+ relationship . Key . SetValue ( oldEntity , relationship . Value ) ; // article.tags = nieuwe lijst
309+ }
310+ }
304311 await _context . SaveChangesAsync ( ) ;
305-
306312 return oldEntity ;
307313 }
308314
@@ -366,7 +372,7 @@ public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string
366372 ? relationship . RelationshipPath
367373 : $ "{ internalRelationshipPath } .{ relationship . RelationshipPath } ";
368374
369- if ( i < relationshipChain . Length )
375+ if ( i < relationshipChain . Length )
370376 entity = _jsonApiContext . ResourceGraph . GetContextEntity ( relationship . Type ) ;
371377 }
372378
0 commit comments