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);
        }
Exemple #2
0
        public virtual ForeignKey?SetForeignKey([CanBeNull] ForeignKey?foreignKey, ConfigurationSource configurationSource)
        {
            EnsureReadonly(false);

            var oldForeignKey = ForeignKey;
            var isChanging    = foreignKey != ForeignKey;

            if (oldForeignKey != null)
            {
                oldForeignKey.ReferencingSkipNavigations !.Remove(this);
            }

            if (foreignKey == null)
            {
                ForeignKey = null;
                _foreignKeyConfigurationSource = null;

                return(isChanging
                    ? (ForeignKey?)DeclaringEntityType.Model.ConventionDispatcher
                       .OnSkipNavigationForeignKeyChanged(Builder, foreignKey, oldForeignKey)
                    : foreignKey);
            }

            var expectedEntityType = IsOnDependent ? foreignKey.DeclaringEntityType : foreignKey.PrincipalEntityType;

            if (expectedEntityType != DeclaringEntityType)
            {
                var message = IsOnDependent
                    ? CoreStrings.SkipNavigationForeignKeyWrongDependentType(
                    foreignKey.Properties.Format(), DeclaringEntityType.DisplayName(), Name, expectedEntityType.DisplayName())
                    : CoreStrings.SkipNavigationForeignKeyWrongPrincipalType(
                    foreignKey.Properties.Format(), DeclaringEntityType.DisplayName(), Name, expectedEntityType.DisplayName());
                throw new InvalidOperationException(message);
            }

            ProcessForeignKey(foreignKey);
            UpdateForeignKeyConfigurationSource(configurationSource);

            if (Inverse?.JoinEntityType != null &&
                Inverse.JoinEntityType != JoinEntityType)
            {
                throw new InvalidOperationException(
                          CoreStrings.SkipInverseMismatchedForeignKey(
                              foreignKey.Properties.Format(),
                              Name, JoinEntityType !.DisplayName(),
                              Inverse.Name, Inverse.JoinEntityType.DisplayName()));
            }

            return(isChanging
                ? (ForeignKey)DeclaringEntityType.Model.ConventionDispatcher
                   .OnSkipNavigationForeignKeyChanged(Builder, foreignKey, oldForeignKey)
                : foreignKey);
        }
Exemple #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>
        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));
            }
        }
Exemple #6
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);
        }
Exemple #7
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>
 public virtual void SetIsReadOnlyAfterSave(bool readOnlyAfterSave, ConfigurationSource configurationSource)
 {
     if (!readOnlyAfterSave &&
         Keys != null)
     {
         throw new InvalidOperationException(CoreStrings.KeyPropertyMustBeReadOnly(Name, DeclaringEntityType.DisplayName()));
     }
     SetFlag(readOnlyAfterSave, PropertyFlags.IsReadOnlyAfterSave);
     UpdateIsReadOnlyAfterSaveConfigurationSource(configurationSource);
 }
Exemple #8
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>
        public virtual void SetIsNullable(bool nullable, ConfigurationSource configurationSource)
        {
            if (nullable)
            {
                if (!ClrType.IsNullableType())
                {
                    throw new InvalidOperationException(CoreStrings.CannotBeNullable(Name, DeclaringEntityType.DisplayName(), ClrType.ShortDisplayName()));
                }

                if (Keys != null)
                {
                    throw new InvalidOperationException(CoreStrings.CannotBeNullablePK(Name, DeclaringEntityType.DisplayName()));
                }
            }

            UpdateIsNullableConfigurationSource(configurationSource);

            var isChanging = IsNullable != nullable;

            SetFlag(nullable, PropertyFlags.IsNullable);
            if (isChanging)
            {
                DeclaringEntityType.Model.ConventionDispatcher.OnPropertyNullableChanged(Builder);
            }
        }
Exemple #9
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>
 public override string ToString()
 => $"'{DeclaringEntityType.DisplayName()}' {Property.Format(Properties)} -> '{PrincipalEntityType.DisplayName()}' {Property.Format(PrincipalKey.Properties)}";
Exemple #10
0
        public virtual void SetIsShadowProperty(bool shadowProperty, ConfigurationSource configurationSource)
        {
            if (IsShadowProperty != shadowProperty)
            {
                if (shadowProperty == false)
                {
                    if (DeclaringEntityType.ClrType == null)
                    {
                        throw new InvalidOperationException(CoreStrings.ClrPropertyOnShadowEntity(Name, DeclaringEntityType.DisplayName()));
                    }

                    var clrProperty = DeclaringEntityType.ClrType.GetPropertiesInHierarchy(Name).FirstOrDefault();
                    if (clrProperty == null)
                    {
                        throw new InvalidOperationException(CoreStrings.NoClrProperty(Name, DeclaringEntityType.DisplayName()));
                    }

                    if (ClrType == null)
                    {
                        ClrType = clrProperty.PropertyType;
                    }
                    else if (ClrType != clrProperty.PropertyType)
                    {
                        throw new InvalidOperationException(CoreStrings.PropertyWrongClrType(Name, DeclaringEntityType.DisplayName()));
                    }
                }

                SetFlag(shadowProperty, PropertyFlags.IsShadowProperty);

                DeclaringEntityType.PropertyMetadataChanged();
            }
            else
            {
                SetFlag(shadowProperty, PropertyFlags.IsShadowProperty);
            }

            UpdateIsShadowPropertyConfigurationSource(configurationSource);
        }
 /// <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 override string ToString()
 // Interpolation okay; strings/debug output
 => $"'{DeclaringEntityType.DisplayName()}' {Property.Format(Properties)} -> '{PrincipalEntityType.DisplayName()}' {Property.Format(PrincipalKey.Properties)}";