/// <summary>
        ///     Sets the key constraint name for this key.
        /// </summary>
        /// <param name="key"> The key. </param>
        /// <param name="name"> The value to set. </param>
        /// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
        /// <returns> The configured name. </returns>
        public static string SetName([NotNull] this IConventionKey key, [CanBeNull] string name, bool fromDataAnnotation = false)
        {
            key.SetOrRemoveAnnotation(
                RelationalAnnotationNames.Name,
                Check.NullButNotEmpty(name, nameof(name)),
                fromDataAnnotation);

            return(name);
        }
Ejemplo n.º 2
0
 public OnForeignKeyPropertiesChangedNode(
     IConventionForeignKeyBuilder relationshipBuilder,
     IReadOnlyList <IConventionProperty> oldDependentProperties,
     IConventionKey oldPrincipalKey)
 {
     RelationshipBuilder    = relationshipBuilder;
     OldDependentProperties = oldDependentProperties;
     OldPrincipalKey        = oldPrincipalKey;
 }
Ejemplo n.º 3
0
 public virtual void ProcessKeyRemoved(
     IConventionEntityTypeBuilder entityTypeBuilder,
     IConventionKey key,
     IConventionContext <IConventionKey> context)
 {
     if (entityTypeBuilder.Metadata.FindPrimaryKey() == null)
     {
         TryConfigurePrimaryKey(entityTypeBuilder);
     }
 }
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual void ProcessKeyRemoved(
     IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey key, IConventionContext <IConventionKey> context)
 {
     foreach (var otherForeignKey in key.DeclaringEntityType.GetDerivedTypesInclusive()
              .SelectMany(t => t.GetDeclaredForeignKeys())
              .Where(fk => AreIndexedBy(fk.Properties, fk.IsUnique, key.Properties, coveringIndexUnique: true)))
     {
         CreateIndex(otherForeignKey.Properties, otherForeignKey.IsUnique, otherForeignKey.DeclaringEntityType.Builder);
     }
 }
        /// <summary>
        ///     Adds a new relationship to this entity.
        /// </summary>
        /// <param name="entityType"> The entity type to add the foreign key to. </param>
        /// <param name="property"> The property that the foreign key is defined on. </param>
        /// <param name="principalKey"> The primary or alternate key that is referenced. </param>
        /// <param name="principalEntityType">
        ///     The entity type that the relationship targets. This may be different from the type that <paramref name="principalKey" />
        ///     is defined on when the relationship targets a derived type in an inheritance hierarchy (since the key is defined on the
        ///     base type of the hierarchy).
        /// </param>
        /// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
        /// <returns> The newly created foreign key. </returns>
        public static IConventionForeignKey AddForeignKey(
            [NotNull] this IConventionEntityType entityType,
            [NotNull] IConventionProperty property,
            [NotNull] IConventionKey principalKey,
            [NotNull] IConventionEntityType principalEntityType,
            bool fromDataAnnotation = false)
        {
            Check.NotNull(entityType, nameof(entityType));

            return(entityType.AddForeignKey(new[] { property }, principalKey, principalEntityType, fromDataAnnotation));
        }
Ejemplo n.º 6
0
 /// <inheritdoc />
 public virtual void ProcessKeyRemoved(
     IConventionEntityTypeBuilder entityTypeBuilder,
     IConventionKey key,
     IConventionContext <IConventionKey> context)
 {
     if (entityTypeBuilder.Metadata.IsKeyless)
     {
         ProcessIdProperty(entityTypeBuilder);
         ProcessJObjectProperty(entityTypeBuilder);
     }
 }
Ejemplo n.º 7
0
 /// <inheritdoc />
 public virtual void ProcessEntityTypePrimaryKeyChanged(
     IConventionEntityTypeBuilder entityTypeBuilder,
     IConventionKey newPrimaryKey,
     IConventionKey previousPrimaryKey,
     IConventionContext <IConventionKey> context)
 {
     if ((newPrimaryKey != null && newPrimaryKey.Properties.Any(p => p.GetJsonPropertyName() == IdPropertyJsonName)) ||
         (previousPrimaryKey != null && previousPrimaryKey.Properties.Any(p => p.GetJsonPropertyName() == IdPropertyJsonName)))
     {
         ProcessIdProperty(entityTypeBuilder);
     }
 }
        /// <summary>
        ///     Called after the foreign key properties or principal key are changed.
        /// </summary>
        /// <param name="relationshipBuilder"> The builder for the foreign key. </param>
        /// <param name="oldDependentProperties"> The old foreign key properties. </param>
        /// <param name="oldPrincipalKey"> The old principal key. </param>
        /// <param name="context"> Additional information associated with convention execution. </param>
        public virtual void ProcessForeignKeyPropertiesChanged(
            IConventionRelationshipBuilder relationshipBuilder,
            IReadOnlyList <IConventionProperty> oldDependentProperties,
            IConventionKey oldPrincipalKey,
            IConventionContext <IConventionRelationshipBuilder> context)
        {
            if (relationshipBuilder.Metadata.Builder == null ||
                relationshipBuilder.Metadata.Properties == oldDependentProperties)
            {
                return;
            }

            ProcessForeignKeyAdded(relationshipBuilder, context);
        }
Ejemplo n.º 9
0
        public virtual void ProcessForeignKeyPropertiesChanged(
            IConventionForeignKeyBuilder relationshipBuilder,
            IReadOnlyList <IConventionProperty> oldDependentProperties,
            IConventionKey oldPrincipalKey,
            IConventionContext <IReadOnlyList <IConventionProperty> > context)
        {
            var foreignKey = relationshipBuilder.Metadata;

            if (foreignKey.IsOwnership &&
                !foreignKey.Properties.SequenceEqual(oldDependentProperties) &&
                relationshipBuilder.Metadata.IsInModel)
            {
                TryConfigurePrimaryKey(foreignKey.DeclaringEntityType.Builder);
            }
        }
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public virtual void ProcessKeyRemoved(
            IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey key, IConventionContext <IConventionKey> context)
        {
            var foreignKeys = key.DeclaringEntityType.GetDerivedTypesInclusive()
                              .SelectMany(t => t.GetDeclaredForeignKeys()).ToList();

            foreach (var foreignKey in foreignKeys)
            {
                if ((!foreignKey.IsUnique ||
                     foreignKey.DeclaringEntityType.BaseType != null))
                {
                    DiscoverProperties(foreignKey.Builder, context);
                }
            }
        }
Ejemplo n.º 11
0
        public virtual void ProcessForeignKeyPropertiesChanged(
            IConventionForeignKeyBuilder relationshipBuilder,
            IReadOnlyList <IConventionProperty> oldDependentProperties,
            IConventionKey oldPrincipalKey,
            IConventionContext <IReadOnlyList <IConventionProperty> > context)
        {
            if (!relationshipBuilder.Metadata.IsInModel ||
                relationshipBuilder.Metadata.Properties == oldDependentProperties)
            {
                return;
            }

            ProcessForeignKey(relationshipBuilder, context);

            context.StopProcessingIfChanged(relationshipBuilder.Metadata.Properties);
        }
        /// <summary>
        ///     Called after the foreign key properties or principal key are changed.
        /// </summary>
        /// <param name="relationshipBuilder"> The builder for the foreign key. </param>
        /// <param name="oldDependentProperties"> The old foreign key properties. </param>
        /// <param name="oldPrincipalKey"> The old principal key. </param>
        /// <param name="context"> Additional information associated with convention execution. </param>
        public virtual void ProcessForeignKeyPropertiesChanged(
            IConventionForeignKeyBuilder relationshipBuilder,
            IReadOnlyList <IConventionProperty> oldDependentProperties,
            IConventionKey oldPrincipalKey,
            IConventionContext <IReadOnlyList <IConventionProperty> > context)
        {
            var foreignKey = relationshipBuilder.Metadata;

            if (!foreignKey.Properties.SequenceEqual(oldDependentProperties))
            {
                OnForeignKeyRemoved(foreignKey.DeclaringEntityType, oldDependentProperties);
                if (relationshipBuilder.Metadata.Builder != null)
                {
                    CreateIndex(foreignKey.Properties, foreignKey.IsUnique, foreignKey.DeclaringEntityType.Builder);
                }
            }
        }
        /// <summary>
        ///     Called after the foreign key properties or principal key are changed.
        /// </summary>
        /// <param name="relationshipBuilder"> The builder for the foreign key. </param>
        /// <param name="oldDependentProperties"> The old foreign key properties. </param>
        /// <param name="oldPrincipalKey"> The old principal key. </param>
        /// <param name="context"> Additional information associated with convention execution. </param>
        public virtual void ProcessForeignKeyPropertiesChanged(
            IConventionRelationshipBuilder relationshipBuilder,
            IReadOnlyList <IConventionProperty> oldDependentProperties,
            IConventionKey oldPrincipalKey,
            IConventionContext <IConventionRelationshipBuilder> context)
        {
            var foreignKey = relationshipBuilder.Metadata;

            if (!foreignKey.Properties.SequenceEqual(oldDependentProperties))
            {
                OnForeignKeyRemoved(oldDependentProperties);

                if (relationshipBuilder.Metadata.Builder != null)
                {
                    foreach (var property in foreignKey.Properties)
                    {
                        property.Builder.ValueGenerated(ValueGenerated.Never);
                    }
                }
            }
        }
Ejemplo n.º 14
0
        public override IReadOnlyList <IConventionProperty>?OnForeignKeyPropertiesChanged(
            IConventionForeignKeyBuilder relationshipBuilder,
            IReadOnlyList <IConventionProperty> oldDependentProperties,
            IConventionKey oldPrincipalKey)
        {
            using (_dispatcher.DelayConventions())
            {
                _propertyListConventionContext.ResetState(relationshipBuilder.Metadata.Properties);
                foreach (var foreignKeyConvention in _conventionSet.ForeignKeyPropertiesChangedConventions)
                {
                    // Some conventions rely on this running even if the relationship has been removed
                    // This will be fixed by reference counting, see #15898
                    //if (relationshipBuilder.Metadata.Builder == null)
                    //{
                    //    return null;
                    //}

                    foreignKeyConvention.ProcessForeignKeyPropertiesChanged(
                        relationshipBuilder, oldDependentProperties, oldPrincipalKey, _propertyListConventionContext);

                    if (_propertyListConventionContext.ShouldStopProcessing())
                    {
                        if (_propertyListConventionContext.Result != null)
                        {
                            // Preserve the old configuration to let the conventions finish processing them
                            _dispatcher.OnForeignKeyPropertiesChanged(relationshipBuilder, oldDependentProperties, oldPrincipalKey);
                        }

                        return(_propertyListConventionContext.Result);
                    }
                }
            }

            if (!relationshipBuilder.Metadata.IsInModel)
            {
                return(null);
            }

            return(relationshipBuilder.Metadata.Properties);
        }
Ejemplo n.º 15
0
        /// <summary>
        ///     Called after the primary key for an entity type is changed.
        /// </summary>
        /// <param name="entityTypeBuilder"> The builder for the entity type. </param>
        /// <param name="newPrimaryKey"> The new primary key. </param>
        /// <param name="previousPrimaryKey"> The old primary key. </param>
        /// <param name="context"> Additional information associated with convention execution. </param>
        public virtual void ProcessEntityTypePrimaryKeyChanged(
            IConventionEntityTypeBuilder entityTypeBuilder,
            IConventionKey newPrimaryKey,
            IConventionKey previousPrimaryKey,
            IConventionContext <IConventionKey> context)
        {
            if (previousPrimaryKey != null)
            {
                foreach (var property in previousPrimaryKey.Properties)
                {
                    property.Builder?.ValueGenerated(ValueGenerated.Never);
                }
            }

            if (newPrimaryKey?.Builder != null)
            {
                foreach (var property in newPrimaryKey.Properties)
                {
                    property.Builder.ValueGenerated(GetValueGenerated(property));
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        ///     Called after the foreign key properties or principal key are changed.
        /// </summary>
        /// <param name="relationshipBuilder">The builder for the foreign key.</param>
        /// <param name="oldDependentProperties">The old foreign key properties.</param>
        /// <param name="oldPrincipalKey">The old principal key.</param>
        /// <param name="context">Additional information associated with convention execution.</param>
        public virtual void ProcessForeignKeyPropertiesChanged(
            IConventionForeignKeyBuilder relationshipBuilder,
            IReadOnlyList <IConventionProperty> oldDependentProperties,
            IConventionKey oldPrincipalKey,
            IConventionContext <IReadOnlyList <IConventionProperty> > context)
        {
            var foreignKey = relationshipBuilder.Metadata;

            if (!foreignKey.Properties.SequenceEqual(oldDependentProperties))
            {
                OnForeignKeyRemoved(oldDependentProperties);

                if (relationshipBuilder.Metadata.IsInModel &&
                    !foreignKey.IsBaseLinking())
                {
                    foreach (var property in foreignKey.Properties)
                    {
                        property.Builder.ValueGenerated(GetValueGenerated(property));
                    }
                }
            }
        }
            public override IConventionKey OnEntityTypePrimaryKeyChanged(
                IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey newPrimaryKey, IConventionKey previousPrimaryKey)
            {
                if (entityTypeBuilder.Metadata.Builder == null)
                {
                    return(null);
                }

                using (_dispatcher.DelayConventions())
                {
                    _keyConventionContext.ResetState(newPrimaryKey);
                    foreach (var keyConvention in _conventionSet.EntityTypePrimaryKeyChangedConventions)
                    {
                        // Some conventions rely on this running even if the new key has been removed
                        // This will be fixed by reference counting, see #15898
                        //if (newPrimaryKey != null && newPrimaryKey.Builder == null)
                        //{
                        //return null;
                        //}

                        keyConvention.ProcessEntityTypePrimaryKeyChanged(
                            entityTypeBuilder, newPrimaryKey, previousPrimaryKey, _keyConventionContext);
                        if (_keyConventionContext.ShouldStopProcessing())
                        {
                            return(_keyConventionContext.Result);
                        }
                    }
                }

                if (newPrimaryKey != null &&
                    newPrimaryKey.Builder == null)
                {
                    return(null);
                }

                return(newPrimaryKey);
            }
Ejemplo n.º 18
0
 public abstract IConventionKey OnEntityTypePrimaryKeyChanged(
     [NotNull] IConventionEntityTypeBuilder entityTypeBuilder,
     [CanBeNull] IConventionKey newPrimaryKey,
     [CanBeNull] IConventionKey previousPrimaryKey);
Ejemplo n.º 19
0
 public abstract IConventionRelationshipBuilder OnForeignKeyPropertiesChanged(
     [NotNull] IConventionRelationshipBuilder relationshipBuilder,
     [NotNull] IReadOnlyList <IConventionProperty> oldDependentProperties,
     [NotNull] IConventionKey oldPrincipalKey);
Ejemplo n.º 20
0
 /// <inheritdoc />
 public void ProcessKeyRemoved(
     IConventionEntityTypeBuilder entityTypeBuilder,
     IConventionKey key,
     IConventionContext <IConventionKey> _)
 => Process(entityTypeBuilder);
Ejemplo n.º 21
0
 public abstract IConventionKey OnKeyRemoved([NotNull] IConventionEntityTypeBuilder entityTypeBuilder, [NotNull] IConventionKey key);
Ejemplo n.º 22
0
 public OnEntityTypePrimaryKeyChangedNode(
     IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey newPrimaryKey, IConventionKey previousPrimaryKey)
 {
     EntityTypeBuilder  = entityTypeBuilder;
     NewPrimaryKey      = newPrimaryKey;
     PreviousPrimaryKey = previousPrimaryKey;
 }
Ejemplo n.º 23
0
 public override IConventionKey OnKeyRemoved(
     IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey key)
 {
     Add(new OnKeyRemovedNode(entityTypeBuilder, key));
     return(key);
 }
 /// <summary>
 ///     Removes a foreign key from this entity type.
 /// </summary>
 /// <param name="entityType"> The entity type to remove the foreign key from. </param>
 /// <param name="properties"> The properties that the foreign key is defined on. </param>
 /// <param name="principalKey"> The primary or alternate key that is referenced. </param>
 /// <param name="principalEntityType">
 ///     The entity type that the relationship targets. This may be different from the type that <paramref name="principalKey" />
 ///     is defined on when the relationship targets a derived type in an inheritance hierarchy (since the key is defined on the
 ///     base type of the hierarchy).
 /// </param>
 /// <returns> The foreign key that was removed. </returns>
 public static IConventionForeignKey RemoveForeignKey(
     [NotNull] this IConventionEntityType entityType,
     [NotNull] IReadOnlyList <IConventionProperty> properties,
     [NotNull] IConventionKey principalKey,
     [NotNull] IConventionEntityType principalEntityType)
 => ((EntityType)entityType).RemoveForeignKey(properties, principalKey, principalEntityType);
 public OnKeyRemovedNode(IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey key)
 {
     EntityTypeBuilder = entityTypeBuilder;
     Key = key;
 }
Ejemplo n.º 26
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual IConventionKey OnKeyRemoved([NotNull] IConventionEntityTypeBuilder entityTypeBuilder, [NotNull] IConventionKey key)
 => _scope.OnKeyRemoved(entityTypeBuilder, key);
Ejemplo n.º 27
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual IConventionKey?OnKeyRemoved(IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey key)
 => _scope.OnKeyRemoved(entityTypeBuilder, key);
Ejemplo n.º 28
0
 /// <summary>
 ///     Gets the <see cref="ConfigurationSource" /> for the constraint name.
 /// </summary>
 /// <param name="key"> The key. </param>
 /// <returns> The <see cref="ConfigurationSource" /> for the constraint name. </returns>
 public static ConfigurationSource?GetNameConfigurationSource([NotNull] this IConventionKey key)
 => key.FindAnnotation(RelationalAnnotationNames.Name)?.GetConfigurationSource();
Ejemplo n.º 29
0
 /// <summary>
 ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
 ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
 ///     any release. You should only use it directly in your code with extreme caution and knowing that
 ///     doing so can result in application failures when updating to a new Entity Framework Core release.
 /// </summary>
 public virtual IConventionKey OnPrimaryKeyChanged(
     [NotNull] IConventionEntityTypeBuilder entityTypeBuilder,
     [CanBeNull] IConventionKey newPrimaryKey,
     [CanBeNull] IConventionKey previousPrimaryKey)
 => _scope.OnEntityTypePrimaryKeyChanged(entityTypeBuilder, newPrimaryKey, previousPrimaryKey);
        public override IConventionKey?OnKeyRemoved(IConventionEntityTypeBuilder entityTypeBuilder, IConventionKey key)
        {
            if (!entityTypeBuilder.Metadata.IsInModel)
            {
                return(null);
            }

            using (_dispatcher.DelayConventions())
            {
                _keyConventionContext.ResetState(key);
                foreach (var keyConvention in _conventionSet.KeyRemovedConventions)
                {
                    keyConvention.ProcessKeyRemoved(entityTypeBuilder, key, _keyConventionContext);
                    if (_keyConventionContext.ShouldStopProcessing())
                    {
                        return(_keyConventionContext.Result);
                    }
                }
            }

            return(key);
        }