I'm using Simple.OData.Client and I would like to update certain properties of an entity.
Let's say I have following class in C#:
[DataContract(Name = "entity")]
public class MyEntity
{
[DataMember(Name = "propertyA")]
public string MyPropertyA { get; set; }
[DataMember(Name = "propertyB")]
public string MyPropertyB { get; set; }
}
I'm trying to update propertyA like this:
await _simpleOdataClient.For<MyEntity>()
.Key(key)
.Set(new MyEntity
{
MyPropertyA = "test"
})
.UpdateEntryAsync();
I took this as an example: https://github.com/object/Simple.OData.Client/wiki/Updating-entries
My problem is that sends a PUT request with propertyA=test but also propertyB=null. It tries to set null value for the property I don't want to change.
Is it possible to only update certain properties and to send HTTP PATCH in the OData request?
You can achieve this with the typed fluent API.
You have to select the fields explicitly for reads (.Select) and set them explicitly on writes.(.Set) to prevent the whole structure being sent.
I think you are almost there.. Instead of .Set(new MyEntity use .Set(new.
I also use a populated entity instance and simplified .Set
is equivalent to
All up then this should work. Unless the DataContract is somehow causing an issue. In my working example there isn't DataContract attributes for the class. Verified with Fiddler that only specified fields are sent ( in my code ).