使用OData服务时出错 - 客户端和服务之间存在类型不匹配

问题描述:

我试图使用OData V4服务。该服务具有此相关的元数据:使用OData服务时出错 - 客户端和服务之间存在类型不匹配

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0"> 
    <edmx:DataServices> 
     <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Some.Name.Space"> 
      <EntityType Name="StatisticalProgram"> 
       <Key> 
        <PropertyRef Name="Id"/> 
       </Key> 
       <Property Name="Name" Type="Edm.String"/> 
       <Property Name="ShortName" Type="Edm.String"/> 
       <Property Name="Deployed" Type="Edm.Boolean" Nullable="false"/> 
       <Property Name="CreatedBy" Type="Edm.String"/> 
       <Property Name="CreatedDate" Type="Edm.DateTimeOffset" Nullable="false"/> 
       <Property Name="UpdateBy" Type="Edm.String"/> 
       <Property Name="UpdatedDate" Type="Edm.DateTimeOffset"/> 
       <Property Name="Id" Type="Edm.Guid" Nullable="false"/> 
      </EntityType> 
     // Other.... 

其中我试图映射到这个模型:

[DataServiceKey("Id")] 
public class StatisticalProgram 
{ 
    public string Name { get; set; } 
    public Guid Id { get; set; } 
    public string ShortName { get; set; } 
} 

我使用招嗅出请求,并请求和响应的外观确定,但我得到这个错误:如果我使用其他来源,像odata.org

There is a type mismatch between the client and the service. Type '[Namespace].StatisticalProgram' is not an entity type, but the type in the response payload represents an entity type. Please ensure that types defined on the client match the data model of the service, or update the service reference on the client.

一切都很正常。

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0"> 
    <edmx:DataServices> 
     <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="ODataDemo"> 
      <EntityType Name="Product"> 
       <Key> 
        <PropertyRef Name="ID"/> 
       </Key> 
       <Property Name="ID" Type="Edm.Int32" Nullable="false"/> 
       <Property Name="Name" Type="Edm.String"/> 
       <Property Name="Description" Type="Edm.String"/> 
       <Property Name="ReleaseDate" Type="Edm.DateTimeOffset" Nullable="false"/> 
       <Property Name="DiscontinuedDate" Type="Edm.DateTimeOffset"/> 
       <Property Name="Rating" Type="Edm.Int16" Nullable="false"/> 
       <Property Name="Price" Type="Edm.Double" Nullable="false"/> 
       <NavigationProperty Name="Categories" Type="Collection(ODataDemo.Category)" Partner="Products"/> 
       <NavigationProperty Name="Supplier" Type="ODataDemo.Supplier" Partner="Products"/> 
       <NavigationProperty Name="ProductDetail" Type="ODataDemo.ProductDetail" Partner="Product"/> 
      </EntityType> 
      // Other 

并映射到这个模型:

public class Product 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
} 

我的客户的消费它的类看起来是这样的:

public class MyClient<T> where T : class 
{ 
    private readonly Uri _uri; 
    private readonly string _entitySetName; 

    public MyClient(Uri uri, string entitySetName) 
    { 
     _uri = uri; 
     _entitySetName = entitySetName; 
    } 

    public IQueryable<T> Entities() 
    { 
     var context = new DataServiceContext(_uri, ODataProtocolVersion.V4); 
     context.Format.LoadServiceModel = GenerateModel; 

     DataServiceQuery<T> query = context.CreateQuery<T>(_entitySetName); 
     return query; 
    } 

    private IEdmModel GenerateModel() 
    { 
     ODataModelBuilder builder = new ODataConventionModelBuilder(); 
     builder.EntitySet<T>(_entitySetName); 
     return builder.GetEdmModel(); 
    } 
} 

我用它像这样Product(伟大工程):

var uri = new Uri("http://services.odata.org/V4/OData/OData.svc"); 
var myClient = new MyClient<Product>(uri, "Products"); 
var result = myClient.Entities().ToList(); 

或者这样的StatisticalProgram(不工作):

var uri = new Uri("http://mylocaluri.com"); 
var myClient = new MyClient<StatisticalProgram>(uri, "StatisticalPrograms"); 
var result = myClient.Entities().ToList(); 

我使用

using System.Web.OData.Builder; 
using Microsoft.OData.Client; 
using Microsoft.OData.Edm; 

所以,总结一下。 如果我使用odata.org,但不是我的本地OData源,它会很好用。我认为它可能与ID属性有关,因为它是​​。这可能会弄乱映射吗?如果您只使用Postman或浏览器,本地OData源代码非常有用。因此,映射似乎存在一些问题。

原来,我用错误的属性来定义Id列。

它应该是:

[global::Microsoft.OData.Client.Key("Id")] 

所以现在我的模型看起来像这样,一切工作正常!

[Microsoft.OData.Client.Key("Id")] 
public class StatisticalProgram 
{ 
    public string Name { get; set; } 
    public Guid Id { get; set; } 
    public string ShortName { get; set; } 
}