Example #1
0
        public virtual Navigation HasDependentToPrincipal(
            [CanBeNull] string name,
            // ReSharper disable once MethodOverloadWithOptionalParameter
            ConfigurationSource configurationSource = ConfigurationSource.Explicit,
            bool runConventions = true)
        {
            var oldNavigation = DependentToPrincipal;

            if (name == oldNavigation?.Name)
            {
                UpdateDependentToPrincipalConfigurationSource(configurationSource);
                return(oldNavigation);
            }

            if (oldNavigation != null)
            {
                Debug.Assert(oldNavigation.Name != null);
                DeclaringEntityType.RemoveNavigation(oldNavigation.Name);
            }

            Navigation navigation = null;

            if (name != null)
            {
                navigation = DeclaringEntityType.AddNavigation(name, this, pointsToPrincipal: true);
            }

            DependentToPrincipal = navigation;
            UpdateDependentToPrincipalConfigurationSource(configurationSource);

            if (runConventions)
            {
                if (oldNavigation != null)
                {
                    Debug.Assert(oldNavigation.Name != null);
                    DeclaringEntityType.Model.ConventionDispatcher.OnNavigationRemoved(
                        DeclaringEntityType.Builder,
                        PrincipalEntityType.Builder,
                        oldNavigation.Name);
                }

                if (navigation != null)
                {
                    navigation = DeclaringEntityType.Model.ConventionDispatcher.OnNavigationAdded(Builder, navigation)
                                 ?.Metadata.DependentToPrincipal;
                }
            }

            return(navigation ?? oldNavigation);
        }
Example #2
0
        public virtual Navigation HasDependentToPrincipal([CanBeNull] string name)
        {
            var oldNavigation = DependentToPrincipal;

            if (oldNavigation != null)
            {
                DeclaringEntityType.RemoveNavigation(oldNavigation.Name);
            }

            Navigation newNavigation = null;

            if (name != null)
            {
                newNavigation = DeclaringEntityType.AddNavigation(name, this, pointsToPrincipal: true);
            }

            DependentToPrincipal = newNavigation;
            return(newNavigation ?? oldNavigation);
        }
Example #3
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        private Navigation Navigation(
            PropertyIdentity?propertyIdentity,
            ConfigurationSource configurationSource,
            bool pointsToPrincipal)
        {
            var name = propertyIdentity?.Name;

            if (pointsToPrincipal &&
                PrincipalEntityType.IsKeyless)
            {
                throw new InvalidOperationException(
                          CoreStrings.NavigationToKeylessType(name, PrincipalEntityType.DisplayName()));
            }

            if (!pointsToPrincipal &&
                DeclaringEntityType.IsKeyless)
            {
                throw new InvalidOperationException(
                          CoreStrings.NavigationToKeylessType(name, DeclaringEntityType.DisplayName()));
            }

            var oldNavigation = pointsToPrincipal ? DependentToPrincipal : PrincipalToDependent;

            if (name == oldNavigation?.Name)
            {
                if (pointsToPrincipal)
                {
                    UpdateDependentToPrincipalConfigurationSource(configurationSource);
                }
                else
                {
                    UpdatePrincipalToDependentConfigurationSource(configurationSource);
                }

                return(oldNavigation);
            }

            if (oldNavigation != null)
            {
                Debug.Assert(oldNavigation.Name != null);
                if (pointsToPrincipal)
                {
                    DeclaringEntityType.RemoveNavigation(oldNavigation.Name);
                }
                else
                {
                    PrincipalEntityType.RemoveNavigation(oldNavigation.Name);
                }
            }

            Navigation navigation = null;
            var        property   = propertyIdentity?.Property;

            if (property != null)
            {
                navigation = pointsToPrincipal
                    ? DeclaringEntityType.AddNavigation(property, this, pointsToPrincipal: true)
                    : PrincipalEntityType.AddNavigation(property, this, pointsToPrincipal: false);
            }
            else if (name != null)
            {
                navigation = pointsToPrincipal
                    ? DeclaringEntityType.AddNavigation(name, this, pointsToPrincipal: true)
                    : PrincipalEntityType.AddNavigation(name, this, pointsToPrincipal: false);
            }

            if (pointsToPrincipal)
            {
                DependentToPrincipal = navigation;
                UpdateDependentToPrincipalConfigurationSource(configurationSource);
            }
            else
            {
                PrincipalToDependent = navigation;
                UpdatePrincipalToDependentConfigurationSource(configurationSource);
            }

            if (oldNavigation != null)
            {
                Debug.Assert(oldNavigation.Name != null);

                if (pointsToPrincipal)
                {
                    DeclaringEntityType.Model.ConventionDispatcher.OnNavigationRemoved(
                        DeclaringEntityType.Builder,
                        PrincipalEntityType.Builder,
                        oldNavigation.Name,
                        oldNavigation.GetIdentifyingMemberInfo());
                }
                else
                {
                    DeclaringEntityType.Model.ConventionDispatcher.OnNavigationRemoved(
                        PrincipalEntityType.Builder,
                        DeclaringEntityType.Builder,
                        oldNavigation.Name,
                        oldNavigation.GetIdentifyingMemberInfo());
                }
            }

            if (navigation != null)
            {
                var builder = DeclaringEntityType.Model.ConventionDispatcher.OnNavigationAdded(Builder, navigation);
                navigation = pointsToPrincipal ? builder?.Metadata.DependentToPrincipal : builder?.Metadata.PrincipalToDependent;
            }

            return(navigation ?? oldNavigation);
        }
Example #4
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        private Navigation Navigation(
            PropertyIdentity?propertyIdentity,
            ConfigurationSource configurationSource,
            bool runConventions,
            bool pointsToPrincipal)
        {
            var name          = propertyIdentity?.Name;
            var oldNavigation = pointsToPrincipal ? DependentToPrincipal : PrincipalToDependent;

            if (name == oldNavigation?.Name)
            {
                if (pointsToPrincipal)
                {
                    UpdateDependentToPrincipalConfigurationSource(configurationSource);
                }
                else
                {
                    UpdatePrincipalToDependentConfigurationSource(configurationSource);
                }
                return(oldNavigation);
            }

            if (oldNavigation != null)
            {
                Debug.Assert(oldNavigation.Name != null);
                if (pointsToPrincipal)
                {
                    DeclaringEntityType.RemoveNavigation(oldNavigation.Name);
                }
                else
                {
                    PrincipalEntityType.RemoveNavigation(oldNavigation.Name);
                }
            }

            Navigation navigation = null;
            var        property   = propertyIdentity?.Property;

            if (property != null)
            {
                navigation = pointsToPrincipal
                    ? DeclaringEntityType.AddNavigation(property, this, pointsToPrincipal: true)
                    : PrincipalEntityType.AddNavigation(property, this, pointsToPrincipal: false);
            }
            else if (name != null)
            {
                navigation = pointsToPrincipal
                    ? DeclaringEntityType.AddNavigation(name, this, pointsToPrincipal: true)
                    : PrincipalEntityType.AddNavigation(name, this, pointsToPrincipal: false);
            }

            if (pointsToPrincipal)
            {
                DependentToPrincipal = navigation;
                UpdateDependentToPrincipalConfigurationSource(configurationSource);
            }
            else
            {
                PrincipalToDependent = navigation;
                UpdatePrincipalToDependentConfigurationSource(configurationSource);
            }

            if (runConventions)
            {
                if (oldNavigation != null)
                {
                    Debug.Assert(oldNavigation.Name != null);

                    if (pointsToPrincipal)
                    {
                        DeclaringEntityType.Model.ConventionDispatcher.OnNavigationRemoved(
                            DeclaringEntityType.Builder,
                            PrincipalEntityType.Builder,
                            oldNavigation.Name,
                            oldNavigation.PropertyInfo);
                    }
                    else
                    {
                        DeclaringEntityType.Model.ConventionDispatcher.OnNavigationRemoved(
                            PrincipalEntityType.Builder,
                            DeclaringEntityType.Builder,
                            oldNavigation.Name,
                            oldNavigation.PropertyInfo);
                    }
                }

                if (navigation != null)
                {
                    var builder = DeclaringEntityType.Model.ConventionDispatcher.OnNavigationAdded(Builder, navigation);
                    navigation = pointsToPrincipal ? builder?.Metadata.DependentToPrincipal : builder?.Metadata.PrincipalToDependent;
                }
            }

            return(navigation ?? oldNavigation);
        }