private object TryFindPrincipal(IStateManager stateManager, INavigation navigation, object dependentEntity) { if (navigation.PointsToPrincipal()) { return(_getterSource.GetAccessor(navigation).GetClrValue(dependentEntity)); } // TODO: Perf foreach (var principalEntry in stateManager.Entries .Where(e => navigation.ForeignKey.PrincipalEntityType.IsAssignableFrom(e.EntityType))) { if (navigation.IsCollection()) { if (_collectionAccessorSource.GetAccessor(navigation).Contains(principalEntry.Entity, dependentEntity)) { return(principalEntry.Entity); } } else if (_getterSource.GetAccessor(navigation).GetClrValue(principalEntry.Entity) == dependentEntity) { return(principalEntry.Entity); } } return(null); }
public virtual void InitializeSets(DbContext context) { foreach (var setInfo in _setFinder.FindSets(context).Where(p => p.HasSetter)) { _setSetters .GetAccessor(setInfo.ContextType, setInfo.Name) .SetClrValue(context, _setSource.Create(context, setInfo.EntityType)); } }
private IEnumerable <IncludedEntity> GetIncludedEntities( object entity, IReadOnlyList <INavigation> navigationPath, int index) { if (index < navigationPath.Count) { var navigation = navigationPath[index]; if (navigation.IsCollection()) { var propertyGetter = _clrPropertyGetterSource.GetAccessor(navigation); var referencedEntities = (IEnumerable <object>)propertyGetter.GetClrValue(entity); foreach (var referencedEntity in referencedEntities.Where(referencedEntity => referencedEntity != null)) { yield return(new IncludedEntity(referencedEntity, _includedEntityTrackingInfos[navigation])); foreach (var includedEntity in GetIncludedEntities(referencedEntity, navigationPath, index + 1)) { yield return(includedEntity); } } } else { var propertyGetter = _clrPropertyGetterSource.GetAccessor(navigation); var referencedEntity = propertyGetter.GetClrValue(entity); if (referencedEntity != null) { yield return(new IncludedEntity(referencedEntity, _includedEntityTrackingInfos[navigation])); foreach (var includedEntity in GetIncludedEntities(referencedEntity, navigationPath, index + 1)) { yield return(includedEntity); } } } } }
private void SetInverse(InternalEntityEntry entry, INavigation navigation, object entity) { var inverse = navigation.FindInverse(); if (inverse != null) { var inverseEntry = entry.StateManager.GetOrCreateEntry(entity); if (inverse.IsCollection()) { var collectionAccessor = _collectionAccessorSource.GetAccessor(inverse); if (!collectionAccessor.Contains(entity, entry.Entity)) { collectionAccessor.Add(entity, entry.Entity); } } else { var oldEntity = _getterSource.GetAccessor(inverse).GetClrValue(entity); if (oldEntity != null && oldEntity != entry.Entity) { var oldEntry = entry.StateManager.GetOrCreateEntry(oldEntity); if (navigation.PointsToPrincipal) { Unfixup(navigation, inverseEntry, oldEntry); SetNullForeignKey(oldEntry, navigation.ForeignKey.Properties); } else { Unfixup(navigation, oldEntry, inverseEntry); } } _setterSource.GetAccessor(inverse).SetClrValue(entity, entry.Entity); } inverseEntry.RelationshipsSnapshot.TakeSnapshot(inverse); } }
private void DoFixup(IEnumerable <INavigation> navigations, InternalEntityEntry principalEntry, InternalEntityEntry[] dependentEntries) { foreach (var navigation in navigations) { if (navigation.PointsToPrincipal) { var setter = _setterSource.GetAccessor(navigation); foreach (var dependent in dependentEntries) { setter.SetClrValue(dependent.Entity, principalEntry.Entity); dependent.RelationshipsSnapshot.TakeSnapshot(navigation); } } else { if (navigation.IsCollection()) { var collectionAccessor = _collectionAccessorSource.GetAccessor(navigation); foreach (var dependent in dependentEntries) { if (!collectionAccessor.Contains(principalEntry.Entity, dependent.Entity)) { collectionAccessor.Add(principalEntry.Entity, dependent.Entity); } } } else { // TODO: Decide how to handle case where multiple values match non-collection nav prop // Issue #739 _setterSource.GetAccessor(navigation).SetClrValue(principalEntry.Entity, dependentEntries.Single().Entity); } principalEntry.RelationshipsSnapshot.TakeSnapshot(navigation); } } }
private void LoadNavigationProperties( object entity, IReadOnlyList <INavigation> navigationPath, int currentNavigationIndex, IReadOnlyList <object> relatedEntities) { if (navigationPath[currentNavigationIndex].PointsToPrincipal() && relatedEntities.Any()) { _clrPropertySetterSource .GetAccessor(navigationPath[currentNavigationIndex]) .SetClrValue(entity, relatedEntities[0]); var inverseNavigation = navigationPath[currentNavigationIndex].FindInverse(); if (inverseNavigation != null) { if (inverseNavigation.IsCollection()) { _clrCollectionAccessorSource .GetAccessor(inverseNavigation) .AddRange(relatedEntities[0], new[] { entity }); } else { _clrPropertySetterSource .GetAccessor(inverseNavigation) .SetClrValue(relatedEntities[0], entity); } } } else { if (navigationPath[currentNavigationIndex].IsCollection()) { _clrCollectionAccessorSource .GetAccessor(navigationPath[currentNavigationIndex]) .AddRange(entity, relatedEntities); var inverseNavigation = navigationPath[currentNavigationIndex].FindInverse(); if (inverseNavigation != null) { var clrPropertySetter = _clrPropertySetterSource .GetAccessor(inverseNavigation); foreach (var relatedEntity in relatedEntities) { clrPropertySetter.SetClrValue(relatedEntity, entity); } } } else if (relatedEntities.Any()) { _clrPropertySetterSource .GetAccessor(navigationPath[currentNavigationIndex]) .SetClrValue(entity, relatedEntities[0]); var inverseNavigation = navigationPath[currentNavigationIndex].FindInverse(); if (inverseNavigation != null) { _clrPropertySetterSource .GetAccessor(inverseNavigation) .SetClrValue(relatedEntities[0], entity); } } } }
public virtual void WriteValue(object entity, IPropertyBase propertyBase, object value) => _setterSource.GetAccessor(propertyBase).SetClrValue(entity, value);
public virtual object ReadValue(object entity, IPropertyBase propertyBase) => _getterSource.GetAccessor(propertyBase).GetClrValue(entity);