@@ -163,7 +163,7 @@ public void DetachRelationshipPointers(TEntity entity)
163
163
{
164
164
foreach ( var hasOneRelationship in _jsonApiContext . HasOneRelationshipPointers . Get ( ) )
165
165
{
166
- var hasOne = ( HasOneAttribute ) hasOneRelationship . Key ;
166
+ var hasOne = ( HasOneAttribute ) hasOneRelationship . Key ;
167
167
if ( hasOne . EntityPropertyName != null )
168
168
{
169
169
var relatedEntity = entity . GetType ( ) . GetProperty ( hasOne . EntityPropertyName ) ? . GetValue ( entity ) ;
@@ -178,7 +178,7 @@ public void DetachRelationshipPointers(TEntity entity)
178
178
179
179
foreach ( var hasManyRelationship in _jsonApiContext . HasManyRelationshipPointers . Get ( ) )
180
180
{
181
- var hasMany = ( HasManyAttribute ) hasManyRelationship . Key ;
181
+ var hasMany = ( HasManyAttribute ) hasManyRelationship . Key ;
182
182
if ( hasMany . EntityPropertyName != null )
183
183
{
184
184
var relatedList = ( IList ) entity . GetType ( ) . GetProperty ( hasMany . EntityPropertyName ) ? . GetValue ( entity ) ;
@@ -194,7 +194,7 @@ public void DetachRelationshipPointers(TEntity entity)
194
194
_context . Entry ( pointer ) . State = EntityState . Detached ;
195
195
}
196
196
}
197
-
197
+
198
198
// HACK: detaching has many relationships doesn't appear to be sufficient
199
199
// the navigation property actually needs to be nulled out, otherwise
200
200
// EF adds duplicate instances to the collection
@@ -234,7 +234,7 @@ private void AttachHasMany(TEntity entity, HasManyAttribute relationship, IList
234
234
{
235
235
_context . Entry ( pointer ) . State = EntityState . Unchanged ;
236
236
}
237
- }
237
+ }
238
238
}
239
239
240
240
private void AttachHasManyThrough ( TEntity entity , HasManyThroughAttribute hasManyThrough , IList pointers )
@@ -270,7 +270,7 @@ private void AttachHasOnePointers(TEntity entity)
270
270
if ( relationship . Key . GetType ( ) != typeof ( HasOneAttribute ) )
271
271
continue ;
272
272
273
- var hasOne = ( HasOneAttribute ) relationship . Key ;
273
+ var hasOne = ( HasOneAttribute ) relationship . Key ;
274
274
if ( hasOne . EntityPropertyName != null )
275
275
{
276
276
var relatedEntity = entity . GetType ( ) . GetProperty ( hasOne . EntityPropertyName ) ? . GetValue ( entity ) ;
@@ -296,13 +296,20 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
296
296
foreach ( var attr in _jsonApiContext . AttributesToUpdate )
297
297
attr . Key . SetValue ( oldEntity , attr . Value ) ;
298
298
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
+ /// Also, we currently do not support the same for many-to-many
307
+ if ( relationship . Key is HasManyAttribute && ! ( relationship . Key is HasManyThroughAttribute ) )
308
+ await _context . Entry ( oldEntity ) . Collection ( relationship . Key . InternalRelationshipName ) . LoadAsync ( ) ;
309
+ relationship . Key . SetValue ( oldEntity , relationship . Value ) ; // article.tags = nieuwe lijst
310
+ }
311
+ }
304
312
await _context . SaveChangesAsync ( ) ;
305
-
306
313
return oldEntity ;
307
314
}
308
315
@@ -366,7 +373,7 @@ public virtual IQueryable<TEntity> Include(IQueryable<TEntity> entities, string
366
373
? relationship . RelationshipPath
367
374
: $ "{ internalRelationshipPath } .{ relationship . RelationshipPath } ";
368
375
369
- if ( i < relationshipChain . Length )
376
+ if ( i < relationshipChain . Length )
370
377
entity = _jsonApiContext . ResourceGraph . GetContextEntity ( relationship . Type ) ;
371
378
}
372
379
0 commit comments