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

Warn if a non-key property is configured as PropertySaveBehavior.Throw #23786

Open
Tracked by #22954
roryap opened this issue Dec 31, 2020 · 3 comments
Open
Tracked by #22954

Warn if a non-key property is configured as PropertySaveBehavior.Throw #23786

roryap opened this issue Dec 31, 2020 · 3 comments

Comments

@roryap
Copy link

roryap commented Dec 31, 2020

See attached project. I am trying to make it so an exception is thrown when a change is made to a certain property after the initial insert. I'm using entity.Property(nameof(MyClass.Name)).Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Throw); but the save is successful (no ex) and the property remains unchanged.

EF Core PropertySaveBehavior.Throw Issue.zip

EF Core version: 5.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10 Pro (1.0.19042)
IDE: Visual Studio 2019 16.8.2

@ajcvickers
Copy link
Member

@roryap The problem here is that EF Core doesn't actually know whether or not the property has been modified or not. This is because entity to be updated is changed while it is not being tracked, and then it is tracked again with a call to Update. Update marks all properties as modified, except that it skips those properties that should not normally be included in a database update. This isn't ideal, but doing otherwise would cause SaveChanges to always throw, which also isn't ideal.

This works correctly if:

  • EF Core is tracking the entity when it is modified and hence marks individual properties as modified when they really do change
  • The application specifically sets modified state on each property based on some other information, such as sending original values to and from the client.

I'm been writing a lot about this recently in the new docs on change tracking--PR here: dotnet/EntityFramework.Docs#2972

We will discuss this behavior, since I agree it is confusing. It may be worthwhile to output a warning if Update is used and any values other than keys are marked as AfterSave.Throw.

@roryap
Copy link
Author

roryap commented Jan 1, 2021

A warning would be great. Just something to trigger some behavior, e.g. throwing my own exception. I'm (obviously) using it in a disconnected way, which I cannot change.

@ajcvickers
Copy link
Member

Note from triage: we will warn if a non-key property is marked to throw after save, but then an attempt is made to set all properties as modified.

@ajcvickers ajcvickers added this to the Backlog milestone Jan 5, 2021
@AndriySvyryd AndriySvyryd changed the title PropertySaveBehavior.Throw not working in at least one case Warn if a non-key property is configured as PropertySaveBehavior.Throw Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants