private object TryFindPrincipal(StateManager stateManager, INavigation navigation, object dependentEntity) { if (navigation.PointsToPrincipal) { return(_getterSource.GetAccessor(navigation).GetClrValue(dependentEntity)); } // TODO: Perf foreach (var principalEntry in stateManager.StateEntries .Where(e => e.EntityType == navigation.ForeignKey.ReferencedEntityType)) { 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); }
private void SetInverse(StateEntry entry, INavigation navigation, object entity) { var inverse = navigation.TryGetInverse(); if (inverse != null) { 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) { Unfixup(navigation, entry.StateManager.GetOrCreateEntry(oldEntity), entry.StateManager.GetOrCreateEntry(entity)); } _setterSource.GetAccessor(inverse).SetClrValue(entity, entry.Entity); } entry.StateManager.GetOrCreateEntry(entity).RelationshipsSnapshot.TakeSnapshot(inverse); } }
public void Delegate_getter_is_cached_by_type_and_property_name() { var entityType = new Model().AddEntityType(typeof(Customer)); var idProperty = entityType.GetOrAddProperty("Id", typeof(int)); var source = new ClrPropertyGetterSource(); var accessor = source.GetAccessor(typeof(Customer), "Id"); Assert.Same(accessor, source.GetAccessor(typeof(Customer), "Id")); Assert.Same(accessor, source.GetAccessor(idProperty)); }
public virtual object ReadValue([NotNull] object entity, [NotNull] IPropertyBase propertyBase) { Check.NotNull(entity, "entity"); Check.NotNull(propertyBase, "propertyBase"); return(_getterSource.GetAccessor(propertyBase).GetClrValue(entity)); }
public void Property_is_returned_if_it_implements_IClrPropertyGetter() { var getterMock = new Mock<IClrPropertyGetter>(); var propertyMock = getterMock.As<IProperty>(); var source = new ClrPropertyGetterSource(); Assert.Same(getterMock.Object, source.GetAccessor(propertyMock.Object)); }
public void Property_is_returned_if_it_implements_IClrPropertyGetter() { var getterMock = new Mock <IClrPropertyGetter>(); var propertyMock = getterMock.As <IProperty>(); var source = new ClrPropertyGetterSource(); Assert.Same(getterMock.Object, source.GetAccessor(propertyMock.Object)); }
private void ConditionallyClearInverse(StateEntry entry, INavigation navigation, object entity) { var inverse = navigation.TryGetInverse(); if (inverse != null) { if (inverse.IsCollection()) { _collectionAccessorSource.GetAccessor(inverse).Remove(entity, entry.Entity); } else { if (ReferenceEquals(_getterSource.GetAccessor(inverse).GetClrValue(entity), entry.Entity)) { _setterSource.GetAccessor(inverse).SetClrValue(entity, null); } } entry.StateManager.GetOrCreateEntry(entity).RelationshipsSnapshot.TakeSnapshot(inverse); } }