Search code examples
c#odata

Transformation error when running OData v4 Client Code Generator


I have an OData service that returns the following from the /$metadata endpoint:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
    <Schema Namespace="(...)" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
      <!-- ... -->
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

When I attempt to run the OData v4 Client Code Generator (v2.3.0) against this XML file, I get the following errors:

Warning: Running transformation: The element 'edmx:Edmx' was unexpected for the root element. The root element should be Edmx.

Warning: The 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata:DataServiceVersion' attribute is not declared.

I also see only an empty .cs output file.

I've tried removing the edmx: namespace prefix from the <Edmx> and <DataServices> elements, making that namespace the default, and adjusting the prefixes on the remaining elements, but that doesn't work either:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Edmx Version="1.0" xmlns="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:edm="http://schemas.microsoft.com/ado/2008/09/edm">
  <DataServices m:DataServiceVersion="1.0">
    <edm:Schema Namespace="(...)">
      <!-- ... -->
    </edm:Schema>
  </DataServices>
</Edmx>

Solution

  • Okay, it looks like the problem might be the version of OData that the service is exposing -- namely, OData v1.0. The edm namespace being defined is http://schemas.microsoft.com/ado/2006/04/edm. See OData Version 4.0 Part 3: Common Schema Definition Language (CSDL), §2.2:

    Elements and attributes that define the entity model exposed by the OData Service are qualified with the Entity Data Model namespace:

    Prior versions of CSDL used the following namespaces for EDM:

    Using the Add Service Reference tool in Visual Studio 2013 (optionally with this update installed) solves the problem of generating client code for this OData service.