Search code examples
c#odataazure-mobile-servicesdelta

How to unset a property on a Delta object


I am using Azure Mobile App Services and in the PATCH method I receive as body an object of type Delta (see MSDN).

I receive some fields with null values that I would like to remove from the Delta input object: how can I do this action?

e.g. I have a JSON input like

{
    "Content":"test",
    "CreatedAt":null
    ...
}

this maps an Entity that inherits from Microsoft.Azure.Mobile.Server.EntityData e.g.

public class MyBean : EntityData
{
    public MyBean() { }
    public string Content { get; set; }
}

I would like to remove the field "CreatedAt", which by the way is declared in the EntityData parent object, part of the Microsoft library (therefore I don't have direct access to it).


Solution

  • I don't think you should try to remove the CreatedAt, but instead, take the incoming Delta and create a new one. You could either include the fields you wanted or exclude the ones you don't want.

    var newDelta = new Delta<MyBean>();
    foreach(var fieldName in patchDelta.GetChangedPropertyNames()){
      if(fieldName != "CreatedAt"){
        if(patchDelta.TryGetPropertyValue(fieldName, out object fieldValue)){
          newDelta.TrySetPropertyValue(fieldNAme,fieldValue);
        }
       }
    }