运行 OData v4 客户端代码生成器时的转换错误

Transformation error when running OData v4 Client Code Generator

我有一个 OData 服务,returns 来自 /$metadata 端点的以下内容:

<?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>

当我尝试 运行 OData v4 Client Code Generator (v2.3.0) 反对这个 XML 文件时,我得到以下错误:

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.

我也只看到一个空的 .cs 输出文件。

我试过从 <Edmx><DataServices> 元素中删除 edmx: 命名空间前缀,使该命名空间成为默认命名空间,并调整其余元素的前缀,但是也不起作用:

<?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>

好的,看起来问题可能出在服务公开的 OData 版本上——即 OData v1.0。正在定义的 edm 命名空间是 http://schemas.microsoft.com/ado/2006/04/edm。见 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:

使用 Add Service Reference 工具 Visual Studio 2013(可选择安装 this update)解决了为此 OData 服务生成客户端代码的问题。