/// <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>
 public virtual InternalRelationshipBuilder Apply(InternalRelationshipBuilder relationshipBuilder, Navigation navigation)
     => Apply(relationshipBuilder);
 private static InversePropertyAttribute GetInversePropertyAttributeOnNavigation(Navigation navigation)
 {
     return navigation.DeclaringEntityType.ClrType?.GetRuntimeProperties()
         .FirstOrDefault(p => string.Equals(p.Name, navigation.Name, StringComparison.OrdinalIgnoreCase))
         ?.GetCustomAttribute<InversePropertyAttribute>(true);
 }
Пример #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 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;
        }