try
{
// code for creating sales order and sales order lines
}
catch (Exception ex)
{
try
{
if (salesOrderCreated)
{
foreach (SalesOrderLine line in salesOrderLineCollection)
{
d365.DeleteObject(line);
}
d365.DeleteObject(soh);
d365.SaveChanges();
}
}
catch (Exception e)
{
throw e;
}
}
at Microsoft.OData.Client.BaseSaveResult.EndRequest() in D:\a\1\s\src\Microsoft.OData.Client\BaseSaveResult.cs:line 304
at Microsoft.OData.Client.DataServiceContext.SaveChanges(SaveChangesOptions options) in D:\a\1\s\src\Microsoft.OData.Client\DataServiceContext.cs:line 2260
at Microsoft.OData.Client.DataServiceContext.SaveChanges() in D:\a\1\s\src\Microsoft.OData.Client\DataServiceContext.cs:line 2239
at myCode.CreateSalesOrder(Resources d365, SalesOrder) in C:\path:line 205
I have tried different ways to resolve this, such as creating a different context and saving using that, or detaching and reattaching the sales order before saving. I was unsuccessful with both these attempt.