protected virtual EntityType ResolveEntityType([NotNull] string entityTypeName)
        {
            if (DeclaringEntityType.Name == entityTypeName)
            {
                return((EntityType)DeclaringEntityType);
            }

            if (RelatedEntityType.Name == entityTypeName)
            {
                return((EntityType)RelatedEntityType);
            }

            if (DeclaringEntityType.DisplayName() == entityTypeName)
            {
                return((EntityType)DeclaringEntityType);
            }

            if (RelatedEntityType.DisplayName() == entityTypeName)
            {
                return((EntityType)RelatedEntityType);
            }

            if (DeclaringEntityType.HasSharedClrType &&
                DeclaringEntityType.ShortName() == entityTypeName)
            {
                return((EntityType)DeclaringEntityType);
            }

            return(RelatedEntityType.HasSharedClrType && RelatedEntityType.ShortName() == entityTypeName
                ? (EntityType)RelatedEntityType
                : null);
        }
        private InternalForeignKeyBuilder WithOneBuilder(MemberIdentity reference)
        {
            if (SkipNavigation != null)
            {
                // Note: we delayed setting the ConfigurationSource of SkipNavigation in HasMany()
                // so we can test it here and override if the skip navigation was originally found
                // by convention.
                if (((IConventionSkipNavigation)SkipNavigation).GetConfigurationSource() == ConfigurationSource.Explicit)
                {
                    throw new InvalidOperationException(
                              CoreStrings.ConflictingRelationshipNavigation(
                                  SkipNavigation.DeclaringEntityType.DisplayName() + "." + SkipNavigation.Name,
                                  RelatedEntityType.DisplayName() + (reference.Name == null
                                                            ? ""
                                                            : "." + reference.Name),
                                  SkipNavigation.DeclaringEntityType.DisplayName() + "." + SkipNavigation.Name,
                                  SkipNavigation.TargetEntityType.DisplayName() + (SkipNavigation.Inverse == null
                                                                            ? ""
                                                                            : "." + SkipNavigation.Inverse.Name)));
                }

                var navigationName      = SkipNavigation.Name;
                var declaringEntityType = (EntityType)DeclaringEntityType;
                declaringEntityType.Model.Builder
                .RemoveAssociationEntityIfCreatedImplicitly(
                    (EntityType)SkipNavigation.AssociationEntityType,
                    removeSkipNavigations: true,
                    ConfigurationSource.Explicit);

                Builder = declaringEntityType.Builder
                          .HasRelationship(
                    (EntityType)RelatedEntityType,
                    navigationName,
                    ConfigurationSource.Explicit,
                    targetIsPrincipal: false);
                SkipNavigation = null;
            }

            var foreignKey    = Builder.Metadata;
            var referenceName = reference.Name;

            if (referenceName != null &&
                foreignKey.DependentToPrincipal != null &&
                foreignKey.GetDependentToPrincipalConfigurationSource() == ConfigurationSource.Explicit &&
                foreignKey.DependentToPrincipal.Name != referenceName)
            {
                InternalForeignKeyBuilder.ThrowForConflictingNavigation(foreignKey, referenceName, newToPrincipal: true);
            }

            return(reference.MemberInfo == null || CollectionMember == null
                ? Builder.HasNavigations(
                       reference.Name, CollectionName,
                       (EntityType)DeclaringEntityType, (EntityType)RelatedEntityType,
                       ConfigurationSource.Explicit)
                : Builder.HasNavigations(
                       reference.MemberInfo, CollectionMember,
                       (EntityType)DeclaringEntityType, (EntityType)RelatedEntityType,
                       ConfigurationSource.Explicit));
        }
Пример #3
0
        private InternalRelationshipBuilder WithOneBuilder(PropertyIdentity reference)
        {
            var referenceName = reference.Name;

            if (!Builder.Metadata.IsUnique &&
                Builder.Metadata.PrincipalToDependent != null &&
                Builder.Metadata.GetPrincipalToDependentConfigurationSource() == ConfigurationSource.Explicit &&
                referenceName != null)
            {
                ThrowForConflictingNavigation(Builder.Metadata, referenceName, false);
            }

            var builder = Builder.IsUnique(true, ConfigurationSource.Explicit);

            if (builder.Metadata.IsSelfReferencing() &&
                referenceName != null &&
                ReferenceName == referenceName)
            {
                throw new InvalidOperationException(CoreStrings.DuplicateNavigation(
                                                        referenceName, RelatedEntityType.DisplayName(), RelatedEntityType.DisplayName()));
            }

            var pointsToPrincipal = !builder.Metadata.IsSelfReferencing() &&
                                    (!builder.Metadata.DeclaringEntityType.IsAssignableFrom(DeclaringEntityType) ||
                                     !builder.Metadata.PrincipalEntityType.IsAssignableFrom(RelatedEntityType) ||
                                     (builder.Metadata.DeclaringEntityType.IsAssignableFrom(RelatedEntityType) &&
                                      builder.Metadata.PrincipalEntityType.IsAssignableFrom(DeclaringEntityType) &&
                                      builder.Metadata.PrincipalToDependent != null &&
                                      builder.Metadata.PrincipalToDependent.Name == ReferenceName));

            if (referenceName != null &&
                ((pointsToPrincipal &&
                  builder.Metadata.DependentToPrincipal != null &&
                  builder.Metadata.GetDependentToPrincipalConfigurationSource() == ConfigurationSource.Explicit &&
                  builder.Metadata.DependentToPrincipal.Name != referenceName) ||
                 (!pointsToPrincipal &&
                  builder.Metadata.PrincipalToDependent != null &&
                  builder.Metadata.GetPrincipalToDependentConfigurationSource() == ConfigurationSource.Explicit &&
                  builder.Metadata.PrincipalToDependent.Name != referenceName)))
            {
                ThrowForConflictingNavigation(builder.Metadata, referenceName, pointsToPrincipal);
            }

            var referenceProperty = reference.Property;

            if (referenceProperty != null)
            {
                return(pointsToPrincipal
                    ? builder.DependentToPrincipal(referenceProperty, ConfigurationSource.Explicit)
                    : builder.PrincipalToDependent(referenceProperty, ConfigurationSource.Explicit));
            }
            else
            {
                return(pointsToPrincipal
                    ? builder.DependentToPrincipal(reference.Name, ConfigurationSource.Explicit)
                    : builder.PrincipalToDependent(reference.Name, ConfigurationSource.Explicit));
            }
        }
Пример #4
0
        private InternalRelationshipBuilder WithOneBuilder(
            MemberIdentity reference,
            Action <NavigationBuilder> navigationConfiguration = null)
        {
            if (SkipNavigation != null)
            {
                throw new InvalidOperationException(
                          CoreStrings.ConflictingRelationshipNavigation(
                              SkipNavigation.DeclaringEntityType.DisplayName() + "." + SkipNavigation.Name,
                              RelatedEntityType.DisplayName() + (reference.Name == null
                                                        ? ""
                                                        : "." + reference.Name),
                              SkipNavigation.DeclaringEntityType.DisplayName() + "." + SkipNavigation.Name,
                              SkipNavigation.TargetEntityType.DisplayName() + (SkipNavigation.Inverse == null
                                                                        ? ""
                                                                        : "." + SkipNavigation.Inverse.Name)));
            }

            var foreignKey    = Builder.Metadata;
            var referenceName = reference.Name;

            if (referenceName != null &&
                foreignKey.DependentToPrincipal != null &&
                foreignKey.GetDependentToPrincipalConfigurationSource() == ConfigurationSource.Explicit &&
                foreignKey.DependentToPrincipal.Name != referenceName)
            {
                InternalRelationshipBuilder.ThrowForConflictingNavigation(foreignKey, referenceName, newToPrincipal: true);
            }

            var withOneBuilder = reference.MemberInfo == null || CollectionMember == null
                ? Builder.HasNavigations(
                reference.Name, CollectionName,
                (EntityType)DeclaringEntityType, (EntityType)RelatedEntityType,
                ConfigurationSource.Explicit)
                : Builder.HasNavigations(
                reference.MemberInfo, CollectionMember,
                (EntityType)DeclaringEntityType, (EntityType)RelatedEntityType,
                ConfigurationSource.Explicit);

            if (navigationConfiguration != null &&
                withOneBuilder.Metadata.DependentToPrincipal != null)
            {
                navigationConfiguration(
                    new NavigationBuilder(withOneBuilder.Metadata.DependentToPrincipal));
            }

            return(withOneBuilder);
        }
Пример #5
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>
        protected virtual EntityType ResolveEntityType([NotNull] string entityTypeName)
        {
            if (DeclaringEntityType.Name == entityTypeName)
            {
                return(DeclaringEntityType);
            }

            if (RelatedEntityType.Name == entityTypeName)
            {
                return(RelatedEntityType);
            }

            if (DeclaringEntityType.DisplayName() == entityTypeName)
            {
                return(DeclaringEntityType);
            }

            return(RelatedEntityType.DisplayName() == entityTypeName ? RelatedEntityType : null);
        }
        private InternalForeignKeyBuilder HasPrincipalKeyBuilder(
            EntityType principalEntityType,
            string principalEntityTypeName,
            Func <InternalForeignKeyBuilder, InternalForeignKeyBuilder> hasPrincipalKey)
        {
            if (principalEntityType == null)
            {
                throw new InvalidOperationException(
                          CoreStrings.PrincipalEntityTypeNotInRelationship(
                              DeclaringEntityType.DisplayName(),
                              RelatedEntityType.DisplayName(),
                              principalEntityTypeName));
            }

            using var batch = principalEntityType.Model.ConventionDispatcher.DelayConventions();
            var builder = Builder.HasEntityTypes(
                principalEntityType, GetOtherEntityType(principalEntityType), ConfigurationSource.Explicit);

            builder = hasPrincipalKey(builder);

            return(batch.Run(builder));
        }
        private InternalRelationshipBuilder HasForeignKeyBuilder(
            EntityType dependentEntityType,
            string dependentEntityTypeName,
            Func <InternalRelationshipBuilder, EntityType, InternalRelationshipBuilder> hasForeignKey)
        {
            if (dependentEntityType == null)
            {
                throw new InvalidOperationException(CoreStrings.DependentEntityTypeNotInRelationship(
                                                        DeclaringEntityType.DisplayName(),
                                                        RelatedEntityType.DisplayName(),
                                                        dependentEntityTypeName));
            }

            using (var batch = dependentEntityType.Model.ConventionDispatcher.StartBatch())
            {
                var builder = Builder.RelatedEntityTypes(
                    GetOtherEntityType(dependentEntityType), dependentEntityType, ConfigurationSource.Explicit);
                builder = hasForeignKey(builder, dependentEntityType);

                return(batch.Run(builder));
            }
        }
Пример #8
0
        /// <summary>
        ///     Returns the internal builder to be used when <see cref="WithOne" /> is called.
        /// </summary>
        /// <param name="reference">
        ///     The name of the reference navigation property on the other end of this relationship.
        ///     If null, there is no navigation property on the other end of the relationship.
        /// </param>
        /// <returns> The internal builder to further configure the relationship. </returns>
        protected virtual InternalRelationshipBuilder WithOneBuilder([CanBeNull] string reference)
        {
            if (Builder.Metadata.IsSelfReferencing() &&
                (reference != null) &&
                (ReferenceName == reference))
            {
                throw new InvalidOperationException(CoreStrings.DuplicateNavigation(
                                                        reference, RelatedEntityType.DisplayName(), RelatedEntityType.DisplayName()));
            }

            var builder           = Builder.IsUnique(true, ConfigurationSource.Explicit);
            var pointsToPrincipal = !builder.Metadata.IsSelfReferencing() &&
                                    (!builder.Metadata.DeclaringEntityType.IsAssignableFrom(DeclaringEntityType) ||
                                     !builder.Metadata.PrincipalEntityType.IsAssignableFrom(RelatedEntityType) ||
                                     (builder.Metadata.DeclaringEntityType.IsAssignableFrom(RelatedEntityType) &&
                                      builder.Metadata.PrincipalEntityType.IsAssignableFrom(DeclaringEntityType) &&
                                      builder.Metadata.PrincipalToDependent != null &&
                                      builder.Metadata.PrincipalToDependent.Name == ReferenceName));

            return(pointsToPrincipal
                ? builder.DependentToPrincipal(reference, ConfigurationSource.Explicit)
                : builder.PrincipalToDependent(reference, ConfigurationSource.Explicit));
        }
        private InternalRelationshipBuilder WithOneBuilder(PropertyIdentity reference)
        {
            var referenceName = reference.Name;

            if (!Builder.Metadata.IsUnique &&
                Builder.Metadata.PrincipalToDependent != null &&
                Builder.Metadata.GetPrincipalToDependentConfigurationSource() == ConfigurationSource.Explicit &&
                referenceName != null)
            {
                ThrowForConflictingNavigation(Builder.Metadata, referenceName, false);
            }

            using (var batch = Builder.Metadata.DeclaringEntityType.Model.ConventionDispatcher.StartBatch())
            {
                var builder    = Builder.IsUnique(true, ConfigurationSource.Explicit);
                var foreignKey = builder.Metadata;
                if (foreignKey.IsSelfReferencing() &&
                    referenceName != null &&
                    ReferenceName == referenceName)
                {
                    throw new InvalidOperationException(
                              CoreStrings.ConflictingPropertyOrNavigation(
                                  referenceName, RelatedEntityType.DisplayName(), RelatedEntityType.DisplayName()));
                }

                var pointsToPrincipal = !foreignKey.IsSelfReferencing() &&
                                        (!foreignKey.DeclaringEntityType.IsAssignableFrom(DeclaringEntityType) ||
                                         !foreignKey.PrincipalEntityType.IsAssignableFrom(RelatedEntityType) ||
                                         (foreignKey.DeclaringEntityType.IsAssignableFrom(RelatedEntityType) &&
                                          foreignKey.PrincipalEntityType.IsAssignableFrom(DeclaringEntityType) &&
                                          foreignKey.PrincipalToDependent != null &&
                                          foreignKey.PrincipalToDependent.Name == ReferenceName));

                if (referenceName != null &&
                    ((pointsToPrincipal &&
                      foreignKey.DependentToPrincipal != null &&
                      foreignKey.GetDependentToPrincipalConfigurationSource() == ConfigurationSource.Explicit &&
                      foreignKey.DependentToPrincipal.Name != referenceName) ||
                     (!pointsToPrincipal &&
                      foreignKey.PrincipalToDependent != null &&
                      foreignKey.GetPrincipalToDependentConfigurationSource() == ConfigurationSource.Explicit &&
                      foreignKey.PrincipalToDependent.Name != referenceName)))
                {
                    ThrowForConflictingNavigation(foreignKey, referenceName, pointsToPrincipal);
                }

                var referenceProperty = reference.MemberInfo;
                if (referenceName != null &&
                    pointsToPrincipal &&
                    RelatedEntityType != foreignKey.DeclaringEntityType)
                {
                    builder = referenceProperty == null && ReferenceProperty == null
                        ? builder.HasNavigations(
                        referenceName, ReferenceName,
                        (EntityType)DeclaringEntityType, (EntityType)RelatedEntityType, ConfigurationSource.Explicit)
                        : builder.HasNavigations(
                        referenceProperty, ReferenceProperty,
                        (EntityType)DeclaringEntityType, (EntityType)RelatedEntityType, ConfigurationSource.Explicit);
                }
                else if (referenceName != null &&
                         !pointsToPrincipal &&
                         RelatedEntityType != foreignKey.PrincipalEntityType)
                {
                    builder = referenceProperty == null && ReferenceProperty == null
                        ? builder.HasNavigations(
                        ReferenceName, referenceName,
                        (EntityType)RelatedEntityType, (EntityType)DeclaringEntityType, ConfigurationSource.Explicit)
                        : builder.HasNavigations(
                        ReferenceProperty, referenceProperty,
                        (EntityType)RelatedEntityType, (EntityType)DeclaringEntityType, ConfigurationSource.Explicit);
                }
                else
                {
                    if (referenceProperty != null)
                    {
                        builder = builder.HasNavigation(
                            referenceProperty,
                            pointsToPrincipal,
                            ConfigurationSource.Explicit);
                    }
                    else
                    {
                        builder = builder.HasNavigation(
                            referenceName,
                            pointsToPrincipal,
                            ConfigurationSource.Explicit);
                    }
                }

                return(batch.Run(builder));
            }
        }