So I've seen the answer to this as being:
modelBuilder.Entity<MyObject>(builder =>
{
builder.Property(e => e.Prop7).Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;
builder.Property(e => e.Prop8).Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;
builder.Property(e => e.Prop9).Metadata.AfterSaveBehavior = PropertySaveBehavior.Ignore;
});
But how can I do this dynamically? How can I stop EF core from updating when I want it to, and allow it to update as usual the majority of the time.
You can set the PropertyEntry.IsModified
to false
which will reset the property's value.
var myObject = ctx.MyObjects.First(); // e.g. myObject.Foo is "foo" in the database
myObject.Foo = "bar";
ctx.Entry(myObject).Property(o => o.Foo).IsModified = false;
// at this point, myObject.Foo is reset to "foo"