2

After quite some googling, you guys have to help now.

Im consuming an Odata Service with the OData v4 Client Code Generator

Everything worked nice and (almost) as it should. Now i need to update an Entity which i have queried and modified before.

The struggle im facing is, i must do a partial update since there are some Properties in this Entity that must not be changed. If the Endpoint detects changes on these props, it throws an exception.

Some time ago i learned, i should be able to do such partial updates and the internet is full of good advices on how to do so. Unfortunately, all of this suggetions refer to EF-6-DataContext and not Odata-DataContext.

Also i do not have much code to show but this

_container.UserSystem.Context.ChangeState(e,EntityStates.Modified);
var resp = _container.SaveChanges(SaveChangesOptions.PostOnlySetProperties);

As you might notice, im setting the SaveChangesOptions.PostOnlySetProperties which im not allowed to (Not working on DataServiceQueries). I already tried this without the SaveChangesOptions.PostOnlySetProperties facing the mentioned Exceptions.

Question

How can i tell that bloody Microsoft.OData.Client.DataServiceContext to perform partial updates and not to update the the whole Entity?

lokusking
  • 7,396
  • 13
  • 38
  • 57
  • Is it an option to move to Web API 2 OData controllers? Then you can use an Entity Framework DbContext and mark individual properties as modified. – Gert Arnold Mar 04 '18 at 11:35

1 Answers1

4

Solved this by myself.

Obviously the only way to achieve this is hidden in this Document from 2014 and nothing has changed since.

Unfortunately one has to use DataServiceCollection to get just the properties updated which were modified.

var col = new DataServiceCollection<User>(_container.UserSystem.Where(x => x.id == entity.Id));
col[0].Name = "test"; //Update Property
_container.SaveChanges();

Personally i find this a spooky and really a bad way to do, but there seems to be no other way.

lokusking
  • 7,396
  • 13
  • 38
  • 57
  • 1
    You saved my day! I could not use PostOnlySetProperties option because the OnPropertyChanged event was not fired. It looked like the fields were not set when submitting to server. Your post make it cleat to me: first I need to create the entity object, then add to collection, and finally set the properties! I was creating object, setting props and adding to collection. – Sebastian Widz Jul 28 '19 at 00:32
  • You don't have to use `DataServiceCollection` but if you don't you will have to implement the _Self-Tracking_ logic within your model classes yourself. It can be done, but it's not a trivial task. Ultimately the `EntityDescriptor` within the `Container` has a private property that holds a list of the properties that should be serialised, you just need to manage that list. Many runtimes that transitioned from the old _Self-Tracking Entities_ https://learn.microsoft.com/en-us/ef/ef6/fundamentals/disconnected-entities/self-tracking-entities/ do this very well. – Chris Schaller Jul 12 '21 at 14:31