I really want to avoid accidental overwrites on a specific property. ValueGenerated.OnAdd
is not useful in this regard because it still tries to set a value in the database (1), unfortunately. I want to essentially make this field read-only so that updates never attempt to overwrite the value that is there. It's an audit-based field that I only need returned upon creation and via select statements.
(1) From EF: (https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.valuegenerated?view=efcore-7.0)
EF may still attempt to save a specific value (rather than having one generated by the database)
Specify the Save Behavior as either "PropertySaveBehavior.Ignore" or "PropertySaveBehavior.Throw" in your modelbuilder depending on which behavior you want:
modelBuilder
.Entity<YourEntity>()
.Property(e => e.YourProperty)
.Metadata.SetAfterSaveBehavior(PropertySaveBehavior.Ignore);
This will allow an initial insert, but no updates to the field afterwards.