-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Allow excluding foreign key from migrations #37815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,4 +173,166 @@ public static bool CanSetConstraintName( | |
| string? name, | ||
| bool fromDataAnnotation = false) | ||
| => relationship.CanSetAnnotation(RelationalAnnotationNames.Name, name, fromDataAnnotation); | ||
|
|
||
| /// <summary> | ||
| /// Configures whether the foreign key constraint is excluded from migrations | ||
| /// when targeting a relational database. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="referenceCollectionBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ReferenceCollectionBuilder ExcludeFromMigrations( | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AndriySvyryd see suggestion to rename this to ExcludeForeignKeyFromMigrations, to make it clear what's being excluded. |
||
| this ReferenceCollectionBuilder referenceCollectionBuilder, | ||
| bool excluded = true) | ||
| { | ||
| referenceCollectionBuilder.Metadata.SetIsExcludedFromMigrations(excluded); | ||
|
|
||
| return referenceCollectionBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures whether the foreign key constraint is excluded from migrations | ||
| /// when targeting a relational database. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="referenceCollectionBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| /// <typeparam name="TEntity">The principal entity type in this relationship.</typeparam> | ||
| /// <typeparam name="TRelatedEntity">The dependent entity type in this relationship.</typeparam> | ||
| public static ReferenceCollectionBuilder<TEntity, TRelatedEntity> ExcludeFromMigrations<TEntity, TRelatedEntity>( | ||
| this ReferenceCollectionBuilder<TEntity, TRelatedEntity> referenceCollectionBuilder, | ||
| bool excluded = true) | ||
| where TEntity : class | ||
| where TRelatedEntity : class | ||
| => (ReferenceCollectionBuilder<TEntity, TRelatedEntity>)ExcludeFromMigrations( | ||
| (ReferenceCollectionBuilder)referenceCollectionBuilder, excluded); | ||
|
|
||
| /// <summary> | ||
| /// Configures whether the foreign key constraint is excluded from migrations | ||
| /// when targeting a relational database. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="referenceReferenceBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ReferenceReferenceBuilder ExcludeFromMigrations( | ||
| this ReferenceReferenceBuilder referenceReferenceBuilder, | ||
| bool excluded = true) | ||
| { | ||
| referenceReferenceBuilder.Metadata.SetIsExcludedFromMigrations(excluded); | ||
|
|
||
| return referenceReferenceBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures whether the foreign key constraint is excluded from migrations | ||
| /// when targeting a relational database. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="referenceReferenceBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| /// <typeparam name="TEntity">The entity type on one end of the relationship.</typeparam> | ||
| /// <typeparam name="TRelatedEntity">The entity type on the other end of the relationship.</typeparam> | ||
| public static ReferenceReferenceBuilder<TEntity, TRelatedEntity> ExcludeFromMigrations<TEntity, TRelatedEntity>( | ||
| this ReferenceReferenceBuilder<TEntity, TRelatedEntity> referenceReferenceBuilder, | ||
| bool excluded = true) | ||
| where TEntity : class | ||
| where TRelatedEntity : class | ||
| => (ReferenceReferenceBuilder<TEntity, TRelatedEntity>)ExcludeFromMigrations( | ||
| (ReferenceReferenceBuilder)referenceReferenceBuilder, excluded); | ||
|
|
||
| /// <summary> | ||
| /// Configures whether the foreign key constraint is excluded from migrations | ||
| /// when targeting a relational database. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="ownershipBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static OwnershipBuilder ExcludeFromMigrations( | ||
| this OwnershipBuilder ownershipBuilder, | ||
| bool excluded = true) | ||
| { | ||
| ownershipBuilder.Metadata.SetIsExcludedFromMigrations(excluded); | ||
|
|
||
| return ownershipBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures whether the foreign key constraint is excluded from migrations | ||
| /// when targeting a relational database. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="ownershipBuilder">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| /// <typeparam name="TEntity">The entity type on one end of the relationship.</typeparam> | ||
| /// <typeparam name="TDependentEntity">The entity type on the other end of the relationship.</typeparam> | ||
| public static OwnershipBuilder<TEntity, TDependentEntity> ExcludeFromMigrations<TEntity, TDependentEntity>( | ||
| this OwnershipBuilder<TEntity, TDependentEntity> ownershipBuilder, | ||
| bool excluded = true) | ||
| where TEntity : class | ||
| where TDependentEntity : class | ||
| => (OwnershipBuilder<TEntity, TDependentEntity>)ExcludeFromMigrations( | ||
| (OwnershipBuilder)ownershipBuilder, excluded); | ||
|
|
||
| /// <summary> | ||
| /// Configures whether the foreign key constraint is excluded from migrations | ||
| /// when targeting a relational database. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="relationship">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> | ||
| /// <returns> | ||
| /// The same builder instance if the configuration was applied, | ||
| /// <see langword="null" /> otherwise. | ||
| /// </returns> | ||
| public static IConventionForeignKeyBuilder? ExcludeFromMigrations( | ||
| this IConventionForeignKeyBuilder relationship, | ||
| bool? excluded, | ||
| bool fromDataAnnotation = false) | ||
| { | ||
| if (!relationship.CanSetExcludeFromMigrations(excluded, fromDataAnnotation)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| relationship.Metadata.SetIsExcludedFromMigrations(excluded, fromDataAnnotation); | ||
| return relationship; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a value indicating whether the foreign key constraint exclusion from migrations can be set | ||
| /// from the current configuration source. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="relationship">The builder being used to configure the relationship.</param> | ||
| /// <param name="excluded">A value indicating whether the foreign key constraint is excluded from migrations.</param> | ||
| /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> | ||
| /// <returns><see langword="true" /> if the configuration can be applied.</returns> | ||
| public static bool CanSetExcludeFromMigrations( | ||
| this IConventionForeignKeyBuilder relationship, | ||
| bool? excluded, | ||
| bool fromDataAnnotation = false) | ||
| => relationship.CanSetAnnotation(RelationalAnnotationNames.IsForeignKeyExcludedFromMigrations, excluded, fromDataAnnotation); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -55,6 +55,11 @@ IReadOnlyList<IColumn> PrincipalColumns | |||||||
| /// </summary> | ||||||||
| ReferentialAction OnDeleteAction { get; } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Gets a value indicating whether the foreign key constraint is excluded from migrations. | ||||||||
| /// </summary> | ||||||||
| bool IsExcludedFromMigrations { get; } | ||||||||
|
||||||||
| bool IsExcludedFromMigrations { get; } | |
| bool IsExcludedFromMigrations | |
| => false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndriySvyryd let me know what you want to do here, AFAIK these interfaces aren't meant to be implemented by external users so this is probably not relevant?
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the new
IsForeignKeyExcludedFromMigrationsfluent API call generation, if the annotation ever ends up explicitly set tofalse(e.g. toggled fromtrueback tofalse), the snapshot will likely emit.ExcludeFromMigrations(false)sinceRemoveAnnotationsHandledByConventions(IForeignKey, …)doesn’t special-case this annotation. Consider removing this annotation when its value isfalse(similar to howIsTableExcludedFromMigrationsis handled) to avoid noisy/unstable snapshots.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AndriySvyryd don't we actually prefer to bake in an explicit false when it's explicitly configured by the user?