C# 使用OData服务时出错-客户端和服务之间存在类型不匹配
我正在尝试使用ODataV4服务。该服务具有以下相关元数据:C# 使用OData服务时出错-客户端和服务之间存在类型不匹配,c#,odata,C#,Odata,我正在尝试使用ODataV4服务。该服务具有以下相关元数据: <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">
<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....
我使用Fiddler嗅探请求,请求和响应看起来都正常,但我得到了以下错误:
客户端和服务之间存在类型不匹配。类型
“[Namespace].StatisticalProgramm”不是实体类型,而是中的类型
响应负载表示实体类型。请确保
客户端上定义的类型与服务的数据模型匹配,或
更新客户端上的服务引用
如果我使用另一个来源,一切都很好,比如
我用于消费它的客户端类如下所示:
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();
}
}
或者类似于统计程序
(不起作用):
总而言之如果我使用odata.org,但不使用我的本地odata源代码,效果会很好。我认为它可能与
ID
-属性相关,因为它是guid
。这会弄乱地图吗?如果您仅将本地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; }
}
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();
}
}
var uri = new Uri("http://services.odata.org/V4/OData/OData.svc");
var myClient = new MyClient<Product>(uri, "Products");
var result = myClient.Entities().ToList();
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;
[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; }
}