Announcements
Hi gurus,
I am facing the following problem. With a C# ODATA tool-generated service, I am unable to delete entities, as opossed to create or update. Im am using a fairly simple code:
DataServiceCollection<PWCustCustomer> customersToDelete = new DataServiceCollection<PWCustCustomer>(context.PWCustCustomers.Where(x => x.AccountNum == accountNumber));
foreach(var customer in customersToDelete)
{
context.ChangeState(customer, EntityStates.Deleted);
}
context.SaveChanges();
When saving, I am getting a 412 HTTP Code "Precondition failed". As per ODATA protocol specs, seems that this error is raised when the odata etag retrieved when instantating the data collection differs from the one sent when sending the delete request, which is the case.
I've no idea on what I am doing wrong here. Note that I've tried as well with context.DeleteObject with exactly the same result, and adding the batch parameter to the SaveChanges method.
Any hints on this issue?
Thanks,
Jorge.
For me the "Precondition failed" error was thrown when trying to do an update. The issue was that I was calling `dataServiceContext.UpdateObject` before `dataServiceContext.SaveChanges`.
foreach (var order in orders)
{
order.Amount = 1;
dataServiceContext.UpdateObject(order);
}
dataServiceContext.SaveChanges();
The call to UpdateObject
is not needed. The data context keeps track of updated entities automatically if the OData generated proxy include change tracking. More details here: github.com/.../181
Just in case some faces this issue.
Problem was that I was not re-creating the context on each service usage.
Adding the a new context in my method did the trick:
public bool CreateCustomer(Customer customer)
{
context = new Resources(oDataUri);
context.SendingRequest2 += new EventHandler<SendingRequest2EventArgs>(delegate (object sender, SendingRequest2EventArgs e)
{
var authenticationHeader = OAuthHelper.GetAuthenticationHeader();
e.RequestMessage.SetHeader(OAuthHelper.OAuthHeader, authenticationHeader);
}
);
DataServiceCollection<PWCustCustomer> customersToDelete = new DataServiceCollection<PWCustCustomer>(context.PWCustCustomers.Where(x => x.AccountNum == accountNumber));
foreach(var customer in customersToDelete)
{
context.ChangeState(customer, EntityStates.Deleted);
}
context.SaveChanges();
André Arnaud de Cal...
294,233
Super User 2025 Season 1
Martin Dráb
232,982
Most Valuable Professional
nmaenpaa
101,158
Moderator