@@ -171,20 +171,14 @@ private void LoadInverseRelationships(object trackedRelationshipValue, Relations
171
171
{
172
172
var relationEntry = _context . Entry ( ( IIdentifiable ) trackedRelationshipValue ) ;
173
173
if ( IsHasOneRelationship ( hasOneAttr . InverseNavigation , trackedRelationshipValue . GetType ( ) ) )
174
- {
175
174
relationEntry . Reference ( hasOneAttr . InverseNavigation ) . Load ( ) ;
176
- }
177
175
else
178
- {
179
176
relationEntry . Collection ( hasOneAttr . InverseNavigation ) . Load ( ) ;
180
- }
181
177
}
182
178
else if ( relationshipAttr is HasManyAttribute hasManyAttr && ! ( relationshipAttr is HasManyThroughAttribute ) )
183
179
{
184
180
foreach ( IIdentifiable relationshipValue in ( IList ) trackedRelationshipValue )
185
- {
186
181
_context . Entry ( relationshipValue ) . Reference ( hasManyAttr . InverseNavigation ) . Load ( ) ;
187
- }
188
182
}
189
183
}
190
184
@@ -193,15 +187,14 @@ private bool IsHasOneRelationship(string internalRelationshipName, Type type)
193
187
var relationshipAttr = _resourceGraph . GetContextEntity ( type ) . Relationships . SingleOrDefault ( r => r . InternalRelationshipName == internalRelationshipName ) ;
194
188
if ( relationshipAttr != null )
195
189
{
196
- if ( relationshipAttr is HasOneAttribute ) return true ;
190
+ if ( relationshipAttr is HasOneAttribute )
191
+ return true ;
192
+
197
193
return false ;
198
194
}
199
- else
200
- {
201
- // relationshipAttr is null when we don't put a [RelationshipAttribute] on the inverse navigation property.
202
- // In this case we use relfection to figure out what kind of relationship is pointing back.
203
- return ! ( type . GetProperty ( internalRelationshipName ) . PropertyType . Inherits ( typeof ( IEnumerable ) ) ) ;
204
- }
195
+ // relationshipAttr is null when we don't put a [RelationshipAttribute] on the inverse navigation property.
196
+ // In this case we use relfection to figure out what kind of relationship is pointing back.
197
+ return ! ( type . GetProperty ( internalRelationshipName ) . PropertyType . Inherits ( typeof ( IEnumerable ) ) ) ;
205
198
}
206
199
207
200
@@ -213,22 +206,16 @@ public void DetachRelationshipPointers(TEntity entity)
213
206
{
214
207
if ( relationshipAttr is HasOneAttribute hasOneAttr )
215
208
{
216
- var relationshipValue = GetEntityResourceSeparationValue ( entity , hasOneAttr ) ?? ( IIdentifiable ) hasOneAttr . GetValue ( entity ) ;
209
+ var relationshipValue = ( IIdentifiable ) hasOneAttr . GetValue ( entity ) ;
217
210
if ( relationshipValue == null ) continue ;
218
211
_context . Entry ( relationshipValue ) . State = EntityState . Detached ;
219
212
}
220
213
else
221
214
{
222
215
IEnumerable < IIdentifiable > relationshipValueList = ( IEnumerable < IIdentifiable > ) relationshipAttr . GetValue ( entity ) ;
223
- /// This adds support for resource-entity separation in the case of one-to-many.
224
- /// todo: currently there is no support for many to many relations.
225
- if ( relationshipAttr is HasManyAttribute hasMany )
226
- relationshipValueList = GetEntityResourceSeparationValue ( entity , hasMany ) ?? relationshipValueList ;
227
216
if ( relationshipValueList == null ) continue ;
228
217
foreach ( var pointer in relationshipValueList )
229
- {
230
218
_context . Entry ( pointer ) . State = EntityState . Detached ;
231
- }
232
219
/// detaching has many relationships is not sufficient to
233
220
/// trigger a full reload of relationships: the navigation
234
221
/// property actually needs to be nulled out, otherwise
@@ -286,45 +273,30 @@ private object GetTrackedRelationshipValue(RelationshipAttribute relationshipAtt
286
273
wasAlreadyAttached = false ;
287
274
if ( relationshipAttr is HasOneAttribute hasOneAttr )
288
275
{
289
- /// This adds support for resource-entity separation in the case of one-to-one.
290
- var relationshipValue = GetEntityResourceSeparationValue ( entity , hasOneAttr ) ?? ( IIdentifiable ) hasOneAttr . GetValue ( entity ) ;
276
+ var relationshipValue = ( IIdentifiable ) hasOneAttr . GetValue ( entity ) ;
291
277
if ( relationshipValue == null )
292
278
return null ;
293
279
return GetTrackedHasOneRelationshipValue ( relationshipValue , hasOneAttr , ref wasAlreadyAttached ) ;
294
280
}
295
- else
296
- {
297
- IEnumerable < IIdentifiable > relationshipValueList = ( IEnumerable < IIdentifiable > ) relationshipAttr . GetValue ( entity ) ;
298
- /// This adds support for resource-entity separation in the case of one-to-many.
299
- /// todo: currently there is no support for many to many relations.
300
- if ( relationshipAttr is HasManyAttribute hasMany )
301
- relationshipValueList = GetEntityResourceSeparationValue ( entity , hasMany ) ?? relationshipValueList ;
302
- if ( relationshipValueList == null ) return null ;
303
- return GetTrackedManyRelationshipValue ( relationshipValueList , relationshipAttr , ref wasAlreadyAttached ) ;
304
- }
281
+
282
+ IEnumerable < IIdentifiable > relationshipValueList = ( IEnumerable < IIdentifiable > ) relationshipAttr . GetValue ( entity ) ;
283
+ if ( relationshipValueList == null )
284
+ return null ;
285
+
286
+ return GetTrackedManyRelationshipValue ( relationshipValueList , relationshipAttr , ref wasAlreadyAttached ) ;
305
287
}
306
288
307
- // helper method used in GetTrackedRelationshipValue. See comments there .
289
+ // helper method used in GetTrackedRelationshipValue. See comments below .
308
290
private IList GetTrackedManyRelationshipValue ( IEnumerable < IIdentifiable > relationshipValueList , RelationshipAttribute relationshipAttr , ref bool wasAlreadyAttached )
309
291
{
310
292
if ( relationshipValueList == null ) return null ;
311
293
bool _wasAlreadyAttached = false ;
312
- /// if we're not using entity resource separation, we can just read off the related type
313
- /// from the RelationshipAttribute. If we DO use separation, RelationshipAttribute.DependentType
314
- /// will point to the Resource, not the Entity, which is not the one we need here.
315
- bool entityResourceSeparation = relationshipAttr . EntityPropertyName != null ;
316
- Type entityType = entityResourceSeparation ? null : relationshipAttr . DependentType ;
317
294
var trackedPointerCollection = relationshipValueList . Select ( pointer =>
318
- {
319
- /// todo: we can't just use relationshipAttr.DependentType because
320
- /// this will point to the Resource type in the case of entity resource
321
- /// separation. We should consider to store entity type on
322
- /// the relationship attribute too.
323
- entityType = entityType ?? pointer . GetType ( ) ;
295
+ { // convert each element in the value list to relationshipAttr.DependentType.
324
296
var tracked = AttachOrGetTracked ( pointer ) ;
325
297
if ( tracked != null ) _wasAlreadyAttached = true ;
326
- return Convert . ChangeType ( tracked ?? pointer , entityType ) ;
327
- } ) . ToList ( ) . Cast ( entityType ) ;
298
+ return Convert . ChangeType ( tracked ?? pointer , relationshipAttr . DependentType ) ;
299
+ } ) . ToList ( ) . Cast ( relationshipAttr . DependentType ) ;
328
300
if ( _wasAlreadyAttached ) wasAlreadyAttached = true ;
329
301
return ( IList ) trackedPointerCollection ;
330
302
}
@@ -513,32 +485,6 @@ private void AssignHasManyThrough(TEntity entity, HasManyThroughAttribute hasMan
513
485
}
514
486
}
515
487
516
- /// <summary>
517
- /// A helper method that gets the relationship value in the case of
518
- /// entity resource separation.
519
- /// </summary>
520
- private IIdentifiable GetEntityResourceSeparationValue ( TEntity entity , HasOneAttribute attribute )
521
- {
522
- if ( attribute . EntityPropertyName == null )
523
- {
524
- return null ;
525
- }
526
- return ( IIdentifiable ) entity . GetType ( ) . GetProperty ( attribute . EntityPropertyName ) ? . GetValue ( entity ) ;
527
- }
528
-
529
- /// <summary>
530
- /// A helper method that gets the relationship value in the case of
531
- /// entity resource separation.
532
- /// </summary>
533
- private IEnumerable < IIdentifiable > GetEntityResourceSeparationValue ( TEntity entity , HasManyAttribute attribute )
534
- {
535
- if ( attribute . EntityPropertyName == null )
536
- {
537
- return null ;
538
- }
539
- return ( ( IEnumerable ) ( entity . GetType ( ) . GetProperty ( attribute . EntityPropertyName ) ? . GetValue ( entity ) ) ) . Cast < IIdentifiable > ( ) ;
540
- }
541
-
542
488
/// <summary>
543
489
/// Given a iidentifiable relationshipvalue, verify if an entity of the underlying
544
490
/// type with the same ID is already attached to the dbContext, and if so, return it.
0 commit comments