You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@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.
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.
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.
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
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
The text was updated successfully, but these errors were encountered: