防止EF Core更新特定实例中的列

时间:2019-06-18 23:21:53

标签: c# .net-core entity-framework-core

所以我认为答案是:

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;
});

但是我该如何动态地做到这一点?如何在需要时阻止EF core更新,并允许它在大多数情况下像往常一样更新。

1 个答案:

答案 0 :(得分:0)

您可以将PropertyEntry.IsModified设置为false,这将重置属性的值。

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"