@@ -214,50 +214,15 @@ private void AttachHasManyThrough(TEntity entity, HasManyThroughAttribute hasMan
214214 foreach ( var pointer in pointers )
215215 {
216216 _context . Entry ( pointer ) . State = EntityState . Unchanged ;
217-
218217 var throughInstance = Activator . CreateInstance ( hasManyThrough . ThroughType ) ;
218+
219219 hasManyThrough . LeftProperty . SetValue ( throughInstance , entity ) ;
220220 hasManyThrough . RightProperty . SetValue ( throughInstance , pointer ) ;
221221
222222 throughRelationshipCollection . Add ( throughInstance ) ;
223223 }
224224 }
225225
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-
261226 /// <summary>
262227 /// This is used to allow creation of HasOne relationships when the
263228 /// independent side of the relationship already exists.
@@ -284,7 +249,7 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
284249 foreach ( var relationship in _jsonApiContext . RelationshipsToUpdate )
285250 relationship . Key . SetValue ( oldEntity , relationship . Value ) ;
286251
287- UpdateHasManyThrough ( entity ) ;
252+ AttachRelationships ( oldEntity ) ;
288253
289254 await _context . SaveChangesAsync ( ) ;
290255
0 commit comments