web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Finance forum / OData generated servic...
Finance forum

OData generated service: "PreConditionFailed" when trying to delete an entity

(0) ShareShare
ReportReport
Posted on by

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.

I have the same question (0)
  • Community Member Profile Picture
    on at

    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();

  • Szilard Profile Picture
    5 on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Finance

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans