2016-08-15 49 views
1

我試圖使用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源代碼非常有用。因此,映射似乎存在一些問題。

回答

1

原來,我用錯誤的屬性來定義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; } 
} 
相關問題