2016-08-15 1 views
1

Ich versuche, einen OData V4-Dienst zu konsumieren. Der Dienst hat diese relevanten Metadaten:Fehler beim OData-Service-Dienst - Es gibt einen Typenkonflikt zwischen dem Client und dem Dienst

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

Was ich zu diesem Modell abzubilden bin versucht:

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

ich Fiddler bin mit dem Wunsch schnuppern, und sowohl die Anfrage und die Antwort aussieht ok, aber ich habe diesen Fehler:

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.

Alles funktioniert gut, wenn ich eine andere Quelle nutzen, wie odata.org.

<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 

Und zu diesem Modell Karte:

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

Meine Client-Klasse es für das Konsumieren wie folgt aussieht:

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(); 
    } 
} 

Und ich benutze es, wie dies für Product (funktioniert gut):

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

Oder, wie dies für StatisticalProgram (funktioniert nicht):

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

I

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

Also, um es zusammenzufassen bin mit. Es funktioniert gut, wenn ich odata.org verwende, aber nicht meine lokale OData-Quelle. Ich denke, es könnte mit der ID -Eigenschaft verwandt sein, da es eine guid ist. Kann das das Mapping durcheinander bringen? Die lokale OData-Quelle funktioniert hervorragend, wenn Sie sie nur mit Postman oder einem Browser verwenden. Es scheint also einige Probleme mit dem Mapping zu geben.

Antwort

1

Es stellte sich heraus, dass ich das falsche Attribut für die Definition der Id-Spalte verwendet habe.

es sein sollte:

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

So jetzt mein Modell dieses und alles sieht aus wie funktioniert gut!

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