Beispiel #1
0
        public virtual Navigation AddNavigation([NotNull] string name, [NotNull] ForeignKey foreignKey, bool pointsToPrincipal)
        {
            Check.NotEmpty(name, nameof(name));
            Check.NotNull(foreignKey, nameof(foreignKey));

            var duplicateNavigation = FindNavigationsInHierarchy(name).FirstOrDefault();

            if (duplicateNavigation != null)
            {
                throw new InvalidOperationException(CoreStrings.DuplicateNavigation(name, DisplayName(), duplicateNavigation.DeclaringEntityType.DisplayName()));
            }

            var duplicateProperty = FindPropertiesInHierarchy(name).FirstOrDefault();

            if (duplicateProperty != null)
            {
                throw new InvalidOperationException(CoreStrings.ConflictingProperty(name, DisplayName(),
                                                                                    duplicateProperty.DeclaringEntityType.DisplayName()));
            }

            var otherNavigation = Navigations.FirstOrDefault(
                n => n.ForeignKey == foreignKey &&
                n.PointsToPrincipal() == pointsToPrincipal);

            if (otherNavigation != null)
            {
                throw new InvalidOperationException(CoreStrings.MultipleNavigations(name, otherNavigation.Name, Name));
            }

            var declaringTypeFromFk = pointsToPrincipal
                ? foreignKey.DeclaringEntityType
                : foreignKey.PrincipalEntityType;

            if (declaringTypeFromFk != this)
            {
                throw new InvalidOperationException(CoreStrings.NavigationOnWrongEntityType(name, Name, declaringTypeFromFk.Name));
            }

            Navigation.IsCompatible(
                name,
                this,
                pointsToPrincipal ? foreignKey.PrincipalEntityType : foreignKey.DeclaringEntityType,
                !pointsToPrincipal && !((IForeignKey)foreignKey).IsUnique,
                shouldThrow: true);

            var navigation = new Navigation(name, foreignKey);

            _navigations.Add(name, navigation);
            if (pointsToPrincipal)
            {
                foreignKey.DependentToPrincipal = navigation;
            }
            else
            {
                foreignKey.PrincipalToDependent = navigation;
            }

            return(navigation);
        }
Beispiel #2
0
        public static bool AreCompatible(
            [NotNull] EntityType principalEntityType,
            [NotNull] EntityType dependentEntityType,
            [CanBeNull] string navigationToPrincipalName,
            [CanBeNull] string navigationToDependentName,
            [CanBeNull] IReadOnlyList <Property> dependentProperties,
            [CanBeNull] IReadOnlyList <Property> principalProperties,
            bool?unique,
            bool?required,
            bool shouldThrow)
        {
            Check.NotNull(principalEntityType, nameof(principalEntityType));
            Check.NotNull(dependentEntityType, nameof(dependentEntityType));

            if (!string.IsNullOrEmpty(navigationToDependentName) &&
                !Navigation.IsCompatible(
                    navigationToDependentName,
                    principalEntityType,
                    dependentEntityType,
                    !unique,
                    shouldThrow))
            {
                return(false);
            }

            if (!string.IsNullOrEmpty(navigationToPrincipalName) &&
                !Navigation.IsCompatible(
                    navigationToPrincipalName,
                    dependentEntityType,
                    principalEntityType,
                    false,
                    shouldThrow))
            {
                return(false);
            }

            if (dependentProperties != null &&
                !CanPropertiesBeRequired(dependentProperties, required, dependentEntityType, true))
            {
                return(false);
            }

            if (principalProperties != null &&
                dependentProperties != null &&
                !AreCompatible(
                    principalProperties,
                    dependentProperties,
                    principalEntityType,
                    dependentEntityType,
                    shouldThrow))
            {
                return(false);
            }

            return(true);
        }