How to update not every fields of an object using Entity Framework and EntityState.Modified

前端 未结 5 1317
鱼传尺愫
鱼传尺愫 2020-12-02 10:49

I need to update all fields except property1 and property2 for the given entity object.
Having this code:

    [HttpPost]
    public ActionResult Add(ob         


        
5条回答
  •  北海茫月
    2020-12-02 11:14

    The answers above (most of them) use DbContext. For those who is using ObjectContext these solutions arent accessible.

    Here is solution for ObjectContext strictly (EF5 .NET 4.5):

    ctx.AddObject("ENTITYNAME", item);
    ctx.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
    
    var entry = ctx.ObjectStateManager.GetObjectStateEntry(item);
    entry.RejectPropertyChanges("PROPERTY_TO_EXCLUDE");
    

提交回复
热议问题