在 EF 核心中执行部分更新并且从不更新某些属性的最佳方法是什么? [英] What is the best way to perform partial updates in EF core and never update certain properties?

查看:50
本文介绍了在 EF 核心中执行部分更新并且从不更新某些属性的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以做类似 var myObj = _db.MyTable.FirstOrDefault(x=>x.Id==id) 的事情,然后更新 myObj 属性您想要更新的属性,但有没有更好的方法来更新 myObj 的 10 个属性中的 6 个,而将其他 4 个单独保留,或者将它们标记为只设置一次且永不设置的方式可以从 ef 核心更新吗?

I know you can do something like var myObj = _db.MyTable.FirstOrDefault(x=>x.Id==id) and then update myObj property by property that you want to update but is there a better way to update say 6 out of 10 properties of myObj and leave the other 4 alone or have them marked as a way that they are only set once and never updateable from ef core?

    public class MyObject
{
    public string Id { get; set; }
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
    public string Prop3 { get; set; }
    public string Prop4 { get; set; }
    public string Prop5 { get; set; }
    public string Prop6 { get; set; }
    public string Prop7 { get; set; }
    public string Prop8 { get; set; }
    public string Prop9 { get; set; }

}

       public void UpdateObj(MyObject ojToUpdate)
    {
        //Is there a better way to write this function if you only want to update a set amount of properties
        var myObj = _db.MyObject.First(x=>x.Id==ojToUpdate.Id);
        myObj.Prop1 = objToUpdate.Prop1;
        myObj.Prop2 = objToUpdate.Prop2;
        myObj.Prop3 = objToUpdate.Prop3;
        myObj.Prop4 = objToUpdate.Prop4;
        myObj.Prop5 = objToUpdate.Prop5;
        myObj.Prop6 = objToUpdate.Prop6;
        _db.SaveChanges();
    }

显然你可以写一些类似_db.MyObject.Update(objToUpdate)的东西.此语句的问题是用户可以更新我不希望他们更新的道具 4/5/6.是的,我知道您可以编写 _db.Entry(myObj).CurrentValues.SetValues(objToUpdate) 然后调用保存更改,但这会覆盖我想要生成一次且不再修改的属性.

Obviously you can write something like _db.MyObject.Update(objToUpdate). The problem with this statement is the user can update prop 4/5/6 which I don't want them to update. Yes I know you can write _db.Entry(myObj).CurrentValues.SetValues(objToUpdate) and then call save changes but that will over ride properties that i want to be generated once and never modified again.

提前致谢.

推荐答案

从 EF Core 2.0 开始,您可以使用 IProperty.AfterSaveBehavior 属性:

Starting with EF Core 2.0, you can use IProperty.AfterSaveBehavior property:

获取一个值,该值指示在实体保存到数据库后是否可以修改此属性.

Gets a value indicating whether or not this property can be modified after the entity is saved to the database.

如果 抛出,那么当实体存在于数据库中后,如果给该属性分配了新的值,则会抛出异常.

If Throw, then an exception will be thrown if a new value is assigned to this property after the entity exists in the database.

如果 忽略,那么对数据库中已经存在的实体的属性值的任何修改都将被忽略.

If Ignore, then any modification to the property value of an entity that already exists in the database will be ignored.

您需要的是 Ignore 选项.在撰写本文时,还没有专门的流畅 API 方法,但是 在更新期间设置显式值 包含如何执行此操作的示例.

What you need is the Ignore option. At the time of writing there is no dedicated fluent API method for that, but Setting an explicit value during update contains an example how you can do that.

以你的例子为例:

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

现在两个

public void UpdateObj(MyObject objToUpdate)
{
    var myObj = _db.MyObject.First(x => x.Id == objToUpdate.Id);
    _db.Entry(myObj).CurrentValues.SetValues(myObjToUpdate);
    _db.SaveChanges();
}

public void UpdateObj(MyObject objToUpdate)
{
    _db.Update(myObjToUpdate);
    _db.SaveChanges();
}

将忽略传递的 myObjToUpdateProp7Prop8Prop9 值.

will ignore Prop7, Prop8 and Prop9 values of the passed myObjToUpdate.

更新(EF Core 3.0+) 上述属性已替换为 GetAfterSaveBehaviorSetAfterSaveBehavior 扩展方法.

Update (EF Core 3.0+) The aforementioned property has been replaced with GetAfterSaveBehavior and SetAfterSaveBehavior extension methods.

这篇关于在 EF 核心中执行部分更新并且从不更新某些属性的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆