-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Attribute for configuring DeleteBehavior #27630
Merged
Merged
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
2929859
Add DeleteBehaviorAttribute
Vekz 25bf1d8
Add DeleteBehaviorAttribute Convention that sets delete behavior on f…
Vekz a903461
Register DeleteBehaviorAttributeConvention in ConventionSet provider
Vekz 88b2376
Add tests for DeleteBehaviorAttribute
Vekz 9ac476a
Make DeleteBehaviorAttribute a property or field attribute
Vekz c410304
Make changes to detect added foreign key
Vekz 3a1693d
Add new registration in ConventionSet provider
Vekz 6a59b7a
Add more test cases for different edge cases
Vekz 7dc5a6f
Add more edge cases to tests
Vekz 46d054b
Add test case for implicit foreign key
Vekz c454830
Cleanup convention and registration in provider
Vekz ac25537
Move DeleteBehavior enum and add TypeForward
Vekz e2aed9a
Make DeleteBehaviorAttribute code and tests use DeleteBehavior enum
Vekz fb34679
Add EFCore.Abstractions assembly to fix tests after TypeForwarding De…
Vekz c0e9b70
Make DeleteBehaviorAttribute descriptions more meaningful
Vekz 95cc27e
Change formatting in DeleteBehaviorAttributeConvention and fix incorr…
Vekz 12d700e
Simplify test suite for DeleteBehaviorAttribute and do some minor for…
Vekz 8168b9f
Make tests reflect setting the attribute on navigation property
Vekz 7e6d766
Take attribute value from being set on dependent navigation property …
Vekz af2e118
Limit use of the attribute to only properties to accomodate only sett…
Vekz 256cb54
Throw InvalidOperationException when DeleteBehaviorAttribute is set o…
Vekz 599f191
Add tests for when Attribute is set on different property than naviga…
Vekz 4c71154
Make sure attribute is set only from dependent side
Vekz 195673a
Make xml comments docs more readable
Vekz a92c66a
Simplify the convention
Vekz 5357529
Add navigation attribute convention tests and adjust code to work cor…
Vekz 8d9d189
Implement 1:1 relations delete behavior setting and restriction to th…
Vekz 3a1da1d
Add 1:1 relationship tests and implement suggestions from PR
Vekz 28b09bb
Add stylistic changes suggested by PR and IDE
Vekz 665fdd8
Fix CoreStrings.resx merge conflicts
Vekz 02860a3
Fix CoreStrings.resx merge conflicts
Vekz 0460b8a
Merge branch 'main' into 9621_DeleteBehaviorAttribute
Vekz 6fc84c1
Run Custom Tool on CoreStrings.Designer.tt
Vekz 4cb7491
Add virtual modifier to the DeleteBehaviorAttributeConvention's publi…
Vekz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Configures the property or field to indicate how a delete operation is applied to dependent entities | ||
/// in a relationship when it is deleted or the relationship is severed. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see> for more information and examples. | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public sealed class DeleteBehaviorAttribute : Attribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DeleteBehaviorAttribute" /> class. | ||
/// </summary> | ||
/// <param name="behavior">The <see cref="DeleteBehavior" /> to be configured.</param> | ||
public DeleteBehaviorAttribute(DeleteBehavior behavior) | ||
{ | ||
Behavior = behavior; | ||
} | ||
|
||
/// <summary> | ||
/// The <see cref="DeleteBehavior" /> to be configured. | ||
/// </summary> | ||
public DeleteBehavior Behavior { get; } | ||
} |
128 changes: 128 additions & 0 deletions
128
src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.EntityFrameworkCore.Metadata.Internal; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
||
/// <summary> | ||
/// A convention that configures the delete behavior based on the <see cref="DeleteBehaviorAttribute" /> applied on the property. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples. | ||
/// </remarks> | ||
public class DeleteBehaviorAttributeConvention : PropertyAttributeConventionBase<DeleteBehaviorAttribute>, INavigationAddedConvention, IForeignKeyPrincipalEndChangedConvention, IModelFinalizingConvention | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="DeleteBehaviorAttributeConvention" />. | ||
/// </summary> | ||
/// <param name="dependencies">Parameter object containing dependencies for this convention.</param> | ||
public DeleteBehaviorAttributeConvention(ProviderConventionSetBuilderDependencies dependencies) | ||
: base(dependencies) { } | ||
|
||
/// <summary> | ||
/// Called after a property is added to the entity type with an attribute on the associated CLR property or field. | ||
/// </summary> | ||
/// <param name="propertyBuilder">The builder for the property.</param> | ||
/// <param name="attribute">The attribute.</param> | ||
/// <param name="clrMember">The member that has the attribute.</param> | ||
/// <param name="context">Additional information associated with convention execution.</param> | ||
protected override void ProcessPropertyAdded( | ||
IConventionPropertyBuilder propertyBuilder, | ||
DeleteBehaviorAttribute attribute, | ||
MemberInfo clrMember, | ||
IConventionContext context) | ||
{ | ||
throw new InvalidOperationException(CoreStrings.DeleteBehaviorAttributeNotOnNavigationProperty); | ||
} | ||
|
||
/// <summary> | ||
/// Called after a navigation is added to the entity type. | ||
/// </summary> | ||
/// <param name="navigationBuilder">The builder for the navigation.</param> | ||
/// <param name="context">Additional information associated with convention execution.</param> | ||
public void ProcessNavigationAdded(IConventionNavigationBuilder navigationBuilder, IConventionContext<IConventionNavigationBuilder> context) | ||
{ | ||
var navAttribute = navigationBuilder.Metadata.PropertyInfo?.GetCustomAttribute<DeleteBehaviorAttribute>(); | ||
if (navAttribute == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (!navigationBuilder.Metadata.IsOnDependent) | ||
{ | ||
throw new InvalidOperationException(CoreStrings.DeleteBehaviorAttributeOnPrincipalProperty); | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var foreignKey = navigationBuilder.Metadata.ForeignKey; | ||
if (foreignKey.IsUnique) | ||
{ | ||
return; | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (foreignKey.GetDeleteBehaviorConfigurationSource() != ConfigurationSource.Explicit) | ||
{ | ||
foreignKey.SetDeleteBehavior(navAttribute.Behavior); | ||
} | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <summary> | ||
/// Called after the principal end of a foreign key is changed. | ||
/// </summary> | ||
/// <param name="relationshipBuilder">The builder for the foreign key.</param> | ||
/// <param name="context">Additional information associated with convention execution.</param> | ||
public void ProcessForeignKeyPrincipalEndChanged(IConventionForeignKeyBuilder relationshipBuilder, IConventionContext<IConventionForeignKeyBuilder> context) | ||
{ | ||
if (!relationshipBuilder.Metadata.IsUnique) | ||
{ | ||
return; | ||
} | ||
|
||
var navigation = relationshipBuilder.Metadata.DependentToPrincipal; | ||
if (navigation == null) | ||
{ | ||
return; | ||
} | ||
|
||
var navAttribute = navigation.PropertyInfo?.GetCustomAttribute<DeleteBehaviorAttribute>(); | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (navAttribute == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (relationshipBuilder.Metadata.GetDeleteBehaviorConfigurationSource() != ConfigurationSource.Explicit) | ||
{ | ||
relationshipBuilder.Metadata.SetDeleteBehavior(navAttribute.Behavior); | ||
} | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// <summary> | ||
/// Called when a model is being finalized. | ||
/// </summary> | ||
/// <param name="modelBuilder">The builder for the model.</param> | ||
/// <param name="context">Additional information associated with convention execution.</param> | ||
public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context) | ||
{ | ||
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes()) | ||
{ | ||
foreach (var navigation in entityType.GetNavigations()) | ||
{ | ||
if (!navigation.ForeignKey.IsUnique) | ||
{ | ||
return; | ||
} | ||
|
||
var navAttribute = navigation.PropertyInfo?.GetCustomAttribute<DeleteBehaviorAttribute>(); | ||
if (navAttribute == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (!navigation.IsOnDependent) | ||
{ | ||
throw new InvalidOperationException(CoreStrings.DeleteBehaviorAttributeOnPrincipalProperty); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.