Skip to content

Attribute for configuring DeleteBehavior - #27630

Merged
AndriySvyryd merged 34 commits into
dotnet:mainfrom
Vekz:9621_DeleteBehaviorAttribute
May 18, 2022
Merged

Attribute for configuring DeleteBehavior#27630
AndriySvyryd merged 34 commits into
dotnet:mainfrom
Vekz:9621_DeleteBehaviorAttribute

Conversation

@Vekz

@Vekz Vekz commented Mar 12, 2022

Copy link
Copy Markdown
Contributor

Fixes #9621

I've added:
DeleteBehaviorAttribute - Stores DeleteBehavior set on the property.
DeleteBehaviorAttributeConvention - Triggers on ForeignKeyAdded, checks if it's properties contain DeleteBehavior. If so then sets DeleteBehavior of this foreign key.
DeleteBehaviorAttributeConventionTest - Tests cases for setting all possible DeleteBehavior for explicit Foreign Key defined by attribute. Also checks for composite key, for multiple different keys with differernt Behaviors, for Foreign Key defined by FluentAPI and for implicit Foreign Key.

Moved:
DeleteBehavior => EFCore.Abstractions and added TypeForward in EFCore
(Also I had to add EFCore.Abstractions reference in base classes for Migrations and Snapshot tests)

@dnfadmin

dnfadmin commented Mar 12, 2022

Copy link
Copy Markdown

CLA assistant check
All CLA requirements met.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a first round of comments, the others will likely have more.

Generally speaking, I think the delete behavior logically applies to the foreign key - or to the navigation - as a whole, and not on each property making up the foreign key. Specifically, the way this PR is currently implemented, it's possible to have multiple conflicting DeleteBehavior values on different properties making up the foreign key.

We should instead consider having the attribute on the navigation property from the dependent side. This would also support the case where the foreign key is a shadow property (but let's see what @AndriySvyryd says before making this change).

Comment thread src/EFCore.Abstractions/DeleteBehavior.cs Outdated
Comment thread src/EFCore.Abstractions/DeleteBehaviorAttribute.cs Outdated
Comment thread src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs Outdated
Comment thread src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs Outdated
Comment thread src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs Outdated
Comment thread test/EFCore.Tests/Metadata/Conventions/DeleteBehaviorAttributeConventionTest.cs Outdated
Comment thread src/EFCore.Abstractions/DeleteBehaviorAttribute.cs Outdated
Comment thread test/EFCore.Tests/Metadata/Conventions/DeleteBehaviorAttributeConventionTest.cs Outdated
Comment thread src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs Outdated
@Vekz

Vekz commented Mar 20, 2022

Copy link
Copy Markdown
Contributor Author

Okay, I'll fix those issues.
Thank you @roji for being so thorough. Also that's great point on configuring DeleteBehavior on navigation properties.
I should've thrown an Exception in current solution.

@AndriySvyryd

AndriySvyryd commented Mar 21, 2022

Copy link
Copy Markdown
Member

We should instead consider having the attribute on the navigation property from the dependent side. This would also support the case where the foreign key is a shadow property (but let's see what @AndriySvyryd says before making this change).

I agree, FK properties could also be used by several FKs.

@Vekz

Vekz commented Mar 23, 2022

Copy link
Copy Markdown
Contributor Author

I've done smaller formatting fixes, simplified convention tests and made attribute being set on navigation property.

@roji How should I go about forcing user to set the attribute on the navigation property. Should I throw an exception if the attribute was set on the individual property of the foreign key instead of the navigation property itself (for now i just ignore that fact)? If so what kind of exception?

Next time I'm gonna have some time I'll do the property attribute tests and clean up the xml docs.

@AndriySvyryd How would you select which entries in xml docs are important enough to keep them as links? Is there some rule of thumb that I should keep in mind?

@roji

roji commented Mar 24, 2022

Copy link
Copy Markdown
Member

How should I go about forcing user to set the attribute on the navigation property. Should I throw an exception if the attribute was set on the individual property of the foreign key instead of the navigation property itself (for now i just ignore that fact)? If so what kind of exception?

Yes, I'd vote for throwing an InvalidOperationException with the message The [DeleteBehavior] attribute may only be specified on navigation properties, and is not supported not on properties making up the foreign key.

How would you select which entries in xml docs are important enough to keep them as links? Is there some rule of thumb that I should keep in mind?

The links here point to very general EF Core concepts (DbContext, SaveChanges) so I think they are quite low-value in general. Just mentioning "context" and <c>SaveChanges</c> should be fine.

@ajcvickers

Copy link
Copy Markdown
Contributor

@Vekz Have you completed all the requested changes such that this is ready for another review?

@Vekz

Vekz commented Apr 4, 2022

Copy link
Copy Markdown
Contributor Author

Sorry I had a lot of work at studies. I still have to clean up XML comments and to add _overrides_configuration_from_convention_source, _does_not_override_configuration_from_explicit_source tests. I was going to do it this till end of the week.

Comment thread src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs Outdated
Comment thread src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs Outdated
Comment thread src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs Outdated
@AndriySvyryd AndriySvyryd self-assigned this May 15, 2022
@Vekz

Vekz commented May 17, 2022

Copy link
Copy Markdown
Contributor Author

@AndriySvyryd I think I've managed to make all your requested changes. Although at the end I merged main branch finally to keep up to date with repo and I think I've broke something as it broke heck a lot of imports. Gonna look into it closer in few days.

modelBuilder.FinalizeModel()).Message
);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line

namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Configures the property or field to indicate how a delete operation is applied to dependent entities

@AndriySvyryd AndriySvyryd May 17, 2022

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Configures the property or field to indicate how a delete operation is applied to dependent entities
/// Configures the navigation property on the dependent side of a relationship to indicate how a delete operation is applied to dependent entities

@Vekz
Vekz force-pushed the 9621_DeleteBehaviorAttribute branch from d7ef011 to 8d9d189 Compare May 18, 2022 16:18
Vekz added 2 commits May 18, 2022 18:18
(cherry picked from commit d7ef011)
(cherry picked from commit 958f49613ac7d0ed1918620a3d6fb1b1ae81b156)

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to resolve the conflict in CoreStrings.resx, you can rebase on latest main and squash your changes. Also make sure to "Run Custom Tool" on CoreStrings.Designer.tt in VS

@Vekz

Vekz commented May 18, 2022

Copy link
Copy Markdown
Contributor Author

Shoot, I use Rider daily at my private PC. So you'll have to wait for me to install VS ;)
Maybe that's why I thought that I broke my branch yesterday, as it seems that Rider doesn't support .NET7 previews at the moment.

@Vekz

Vekz commented May 18, 2022

Copy link
Copy Markdown
Contributor Author

Okay, I've run the Custom Tool and I hope fixed tests that weren't passing on CIs.

@AndriySvyryd
AndriySvyryd merged commit 0a125ab into dotnet:main May 18, 2022
@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

@Vekz

Vekz commented May 18, 2022

Copy link
Copy Markdown
Contributor Author

Thank you for your help with this issue. Especially that it was my first ever open source issue. It was really educative experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeleteBehaviorAttribute

5 participants