@@ -214,50 +214,15 @@ private void AttachHasManyThrough(TEntity entity, HasManyThroughAttribute hasMan
214
214
foreach ( var pointer in pointers )
215
215
{
216
216
_context . Entry ( pointer ) . State = EntityState . Unchanged ;
217
-
218
217
var throughInstance = Activator . CreateInstance ( hasManyThrough . ThroughType ) ;
218
+
219
219
hasManyThrough . LeftProperty . SetValue ( throughInstance , entity ) ;
220
220
hasManyThrough . RightProperty . SetValue ( throughInstance , pointer ) ;
221
221
222
222
throughRelationshipCollection . Add ( throughInstance ) ;
223
223
}
224
224
}
225
225
226
- private void UpdateHasManyThrough ( TEntity entity )
227
- {
228
- var relationships = _jsonApiContext . HasManyRelationshipPointers . Get ( ) ;
229
- foreach ( var relationship in relationships )
230
- {
231
- if ( relationship . Key is HasManyThroughAttribute hasManyThrough )
232
- {
233
- // create the collection (e.g. List<ArticleTag>)
234
- // this type MUST implement IList so we can build the collection
235
- // if this is problematic, we _could_ reflect on the type and find an Add method
236
- // or we might be able to create a proxy type and implement the enumerator
237
- var throughRelationshipCollection = Activator . CreateInstance ( hasManyThrough . ThroughProperty . PropertyType ) as IList ;
238
- hasManyThrough . ThroughProperty . SetValue ( entity , throughRelationshipCollection ) ;
239
- foreach ( var pointer in relationship . Value )
240
- {
241
- _context . Entry ( pointer ) . State = EntityState . Unchanged ;
242
-
243
- var throughInstance = Activator . CreateInstance ( hasManyThrough . ThroughType ) ;
244
- _context . Entry ( throughInstance ) . State = EntityState . Added ;
245
-
246
- hasManyThrough . LeftIdProperty . SetValue ( throughInstance , entity . Id ) ;
247
- hasManyThrough . LeftProperty . SetValue ( throughInstance , entity ) ;
248
- hasManyThrough . RightProperty . SetValue ( throughInstance , pointer ) ;
249
-
250
- var pointerId = ( pointer as Identifiable < TId > ) // known limitation, just need to come up with a solution...
251
- ?? throw new JsonApiException ( 500 , $ "Cannot update the HasManyThrough relationship '{ hasManyThrough . PublicRelationshipName } '. Id type must match the parent resource id type.") ;
252
-
253
- hasManyThrough . RightIdProperty . SetValue ( throughInstance , pointerId . Id ) ;
254
-
255
- throughRelationshipCollection . Add ( throughInstance ) ;
256
- }
257
- }
258
- }
259
- }
260
-
261
226
/// <summary>
262
227
/// This is used to allow creation of HasOne relationships when the
263
228
/// independent side of the relationship already exists.
@@ -284,7 +249,7 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
284
249
foreach ( var relationship in _jsonApiContext . RelationshipsToUpdate )
285
250
relationship . Key . SetValue ( oldEntity , relationship . Value ) ;
286
251
287
- UpdateHasManyThrough ( entity ) ;
252
+ AttachRelationships ( oldEntity ) ;
288
253
289
254
await _context . SaveChangesAsync ( ) ;
290
255
0 commit comments