2016-08-15 6 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; } 
} 

私は要求を盗聴するフィドラーを使用していますが、リクエストとレスポンスのルックスの両方[OK]を、私はこのエラーを取得:私は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のプロパティに関連している可能性があります。それはマッピングを混乱させることができますか?ローカルのODataソースは、Postmanまたはブラウザでのみ使用すると効果的です。だから、マッピングにいくつかの問題があるようです。

答えて

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; } 
} 
関連する問題