Skip to content
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 34 commits into from
May 18, 2022

Conversation

Vekz
Copy link
Contributor

@Vekz Vekz commented Mar 12, 2022

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
Copy link

dnfadmin commented Mar 12, 2022

CLA assistant check
All CLA requirements met.

Copy link
Member

@roji roji left a comment

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).

@Vekz
Copy link
Contributor Author

Vekz commented Mar 20, 2022

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
Copy link
Member

AndriySvyryd commented Mar 21, 2022

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
Copy link
Contributor Author

Vekz commented Mar 23, 2022

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
Copy link
Member

roji commented Mar 24, 2022

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
Member

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

@Vekz
Copy link
Contributor Author

Vekz commented Apr 4, 2022

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.

@AndriySvyryd AndriySvyryd self-assigned this May 15, 2022
@Vekz
Copy link
Contributor Author

Vekz commented May 17, 2022

@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
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
Copy link
Member

@AndriySvyryd AndriySvyryd May 17, 2022

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)
Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

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
Copy link
Contributor Author

Vekz commented May 18, 2022

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
Copy link
Contributor Author

Vekz commented May 18, 2022

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
Member

Thanks for your contribution!

@Vekz
Copy link
Contributor Author

Vekz commented May 18, 2022

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