After upgrading CRM from 2016 to 365 on premise, I now get the error "Attribute 'ordernumber' cannot be null" (on the last line) when creating an order using the following code.

public static Entity PersistOrder(IOrganizationService service, Entity order, List<Entity> orderProducts, List<Entity> orderProductsNotInvoice)
{
Relationship orderRelashionship = new Relationship("order_details");
EntityCollection orderProductsCollection = new EntityCollection(orderProducts);
order.RelatedEntities.Add(orderRelashionship, orderProductsCollection);

ExecuteTransactionRequest request = new ExecuteTransactionRequest()
{
Requests = new OrganizationRequestCollection()
};
CreateRequest createRequest = new CreateRequest() { Target = order };
request.Requests.Add(createRequest);
var response = (ExecuteTransactionResponse)service.Execute(request);

Auto numbering is working fine when an order is created in CRM itself. As a test I created an OrganizationServiceProxy using hard coded credentials and it also successfully created the order. Even if I use service.Create(order) I still get the error. Any ideas why auto numbering no longer works with IOrganizationService after the upgrade?