Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
                    }
                }
            }
        }
        private void InitialFixup(InternalEntityEntry entry)
        {
            var entityType = entry.EntityType;

            foreach (var navigation in entityType.GetNavigations())
            {
                var navigationValue = entry[navigation];

                if (navigationValue != null)
                {
                    if (navigation.IsCollection())
                    {
                        NavigationCollectionChangedAction(
                            entry,
                            navigation,
                            ((IEnumerable)navigationValue).Cast <object>().ToList(),
                            Enumerable.Empty <object>());
                    }
                    else
                    {
                        NavigationReferenceChangedAction(
                            entry,
                            navigation,
                            null,
                            navigationValue);
                    }
                }
            }

            var entries = entry.StateManager.Entries.ToList();

            // TODO: Perf on this state manager query
            foreach (var navigation in _model.EntityTypes
                     .SelectMany(e => e.GetNavigations())
                     .Where(n => n.GetTargetType() == entityType))
            {
                IClrCollectionAccessor collectionAccessor = null;
                if (navigation.IsCollection())
                {
                    collectionAccessor = _collectionAccessorSource.GetAccessor(navigation);
                }

                var navigationEntityType = navigation.EntityType;

                foreach (var relatedEntry in entries)
                {
                    if (relatedEntry.EntityType != navigationEntityType ||
                        relatedEntry == entry)
                    {
                        continue;
                    }

                    if (collectionAccessor != null)
                    {
                        if (collectionAccessor.Contains(relatedEntry.Entity, entry.Entity))
                        {
                            NavigationCollectionChangedAction(
                                relatedEntry,
                                navigation,
                                new[] { entry.Entity },
                                Enumerable.Empty <object>());
                        }
                    }
                    else
                    {
                        var navigationValue = relatedEntry[navigation];

                        if (navigationValue != null)
                        {
                            if (ReferenceEquals(navigationValue, entry.Entity))
                            {
                                NavigationReferenceChangedAction(
                                    relatedEntry,
                                    navigation,
                                    null,
                                    navigationValue);
                            }
                        }
                    }
                }
            }

            foreach (var foreignKey in entityType.GetForeignKeys())
            {
                var principalEntry = entry.StateManager.GetPrincipal(entry.RelationshipsSnapshot, foreignKey);
                if (principalEntry != null)
                {
                    DoFixup(foreignKey, principalEntry, new[] { entry });
                }
            }

            foreach (var foreignKey in _model.GetReferencingForeignKeys(entityType))
            {
                var dependents = entry.StateManager.GetDependents(entry, foreignKey).ToArray();

                if (dependents.Length > 0)
                {
                    DoFixup(foreignKey, entry, dependents);
                }
            }
        }
Exemplo n.º 4
0
        private void InitialFixup(InternalEntityEntry entry)
        {
            var entityType = entry.EntityType;

            // If the new state is unchanged (such as from a query or Attach) then we are going
            // to assume that the FK value is the source of truth and not attempt to ascertain
            // relationships from navigation properties
            if (entry.EntityState != EntityState.Unchanged)
            {
                foreach (var navigation in entityType.GetNavigations())
                {
                    var navigationValue = entry[navigation];
                    if (navigationValue != null)
                    {
                        if (navigation.IsCollection())
                        {
                            NavigationCollectionChangedAction(
                                entry,
                                navigation,
                                ((IEnumerable)navigationValue).Cast <object>().ToList(),
                                Enumerable.Empty <object>());
                        }
                        else
                        {
                            NavigationReferenceChangedAction(
                                entry,
                                navigation,
                                null,
                                navigationValue);
                        }
                    }
                }

                var entries = entry.StateManager.Entries.ToList();

                // TODO: Perf on this state manager query
                foreach (var navigation in _model.EntityTypes
                         .SelectMany(e => e.GetNavigations())
                         .Where(n => n.GetTargetType().IsAssignableFrom(entityType)))
                {
                    IClrCollectionAccessor collectionAccessor = null;
                    if (navigation.IsCollection())
                    {
                        collectionAccessor = _collectionAccessorSource.GetAccessor(navigation);
                    }

                    var navigationEntityType = navigation.DeclaringEntityType;

                    foreach (var relatedEntry in entries)
                    {
                        if (!navigationEntityType.IsAssignableFrom(relatedEntry.EntityType) ||
                            relatedEntry == entry)
                        {
                            continue;
                        }

                        if (collectionAccessor != null)
                        {
                            if (collectionAccessor.Contains(relatedEntry.Entity, entry.Entity))
                            {
                                NavigationCollectionChangedAction(
                                    relatedEntry,
                                    navigation,
                                    new[] { entry.Entity },
                                    Enumerable.Empty <object>());
                            }
                        }
                        else
                        {
                            var navigationValue = relatedEntry[navigation];
                            if (navigationValue != null)
                            {
                                if (ReferenceEquals(navigationValue, entry.Entity))
                                {
                                    NavigationReferenceChangedAction(
                                        relatedEntry,
                                        navigation,
                                        null,
                                        navigationValue);
                                }
                            }
                        }
                    }
                }
            }

            foreach (var foreignKey in entityType.GetForeignKeys())
            {
                var principalEntry = entry.StateManager.GetPrincipal(entry.RelationshipsSnapshot, foreignKey);
                if (principalEntry != null)
                {
                    DoFixup(foreignKey, principalEntry, new[] { entry });
                }
            }

            foreach (var foreignKey in _model.FindReferencingForeignKeys(entityType))
            {
                var dependents = entry.StateManager.GetDependents(entry, foreignKey).ToArray();
                if (dependents.Length > 0)
                {
                    DoFixup(foreignKey, entry, dependents);
                }
            }
        }