Prevent EF Core From Updating A Column In A Specific Instance

.net-core c# entity-framework-core

Question

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.

1
0
6/18/2019 11:21:53 PM

Popular Answer

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"
0
6/19/2019 1:31:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow