什么是“ID”属性确切地用于OData

时间:2014-01-07 15:54:20

标签: c# odata wcf-data-services

我想问一下OData中使用的“<d:ID>”属性是什么?

我附加了样本服务,其中存在“ID”(它被定义为实体密钥“)。(参见下面的代码块)

我们在响应xml中嵌套了条目,例如产品&amp;分类。产品包含类别(请参阅下面的代码块)

实施例: 假设我有第一个产品“电视”,它属于2个不同类别“电气”“ID = 1”&amp; “电子产品”“ID = 2”...第二产品“牛奶”属于“饮料”类别“ID = 1”&amp; “喝”“ID = 5”,我在同一回复中获得所有这些数据。

如果您在示例中看到“电气”&amp; “饮料”具有相同的ID(即1),但它们存在于不同的产品中。这种情况可能吗?

代码块:

 <?xml version="1.0" encoding="utf-8" ?> 
    - <feed xml:base="http://services.odata.org/V3/OData/OData.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
      <id>http://services.odata.org/V3/OData/OData.svc/Products</id> 
      <title type="text">Products</title> 
      <updated>2014-01-07T15:47:23Z</updated> 
      <link rel="self" title="Products" href="Products" /> 
    - <entry>
      <id>http://services.odata.org/V3/OData/OData.svc/Products(0)</id> 
      <category term="ODataDemo.Product" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" title="Product" href="Products(0)" /> 
    - <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories" type="application/atom+xml;type=feed" title="Categories" href="Products(0)/Categories">
    - <m:inline>
    - <feed>
      <id>http://services.odata.org/V3/OData/OData.svc/Products(0)/Categories</id> 
      <title type="text">Categories</title> 
      <updated>2014-01-07T15:47:23Z</updated> 
      <link rel="self" title="Categories" href="Products(0)/Categories" /> 
    - <entry>
      <id>http://services.odata.org/V3/OData/OData.svc/Categories(0)</id> 
      <category term="ODataDemo.Category" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" title="Category" href="Categories(0)" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed" title="Products" href="Categories(0)/Products" /> 
      <title type="text">Food</title> 
      <updated>2014-01-07T15:47:23Z</updated> 
    - <author>
      <name /> 
      </author>
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Products" type="application/xml" title="Products" href="Categories(0)/$links/Products" /> 
    - <content type="application/xml">
    - <m:properties>
      <d:ID m:type="Edm.Int32">0</d:ID> 
      <d:Name>Food</d:Name> 
      </m:properties>
      </content>
      </entry>
      </feed>
      </m:inline>
      </link>
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Supplier" type="application/atom+xml;type=entry" title="Supplier" href="Products(0)/Supplier" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ProductDetail" type="application/atom+xml;type=entry" title="ProductDetail" href="Products(0)/ProductDetail" /> 
      <title type="text">Bread</title> 
      <summary type="text">Whole grain bread</summary> 
      <updated>2014-01-07T15:47:23Z</updated> 
    - <author>
      <name /> 
      </author>
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Categories" type="application/xml" title="Categories" href="Products(0)/$links/Categories" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Supplier" type="application/xml" title="Supplier" href="Products(0)/$links/Supplier" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/ProductDetail" type="application/xml" title="ProductDetail" href="Products(0)/$links/ProductDetail" /> 
    - <content type="application/xml">
    - <m:properties>
      <d:ID m:type="Edm.Int32">0</d:ID> 
      <d:ReleaseDate m:type="Edm.DateTimeOffset">1992-01-01T00:00:00Z</d:ReleaseDate> 
      <d:DiscontinuedDate m:null="true" /> 
      <d:Rating m:type="Edm.Int16">4</d:Rating> 
      <d:Price m:type="Edm.Double">2.5</d:Price> 
      </m:properties>
      </content>
      </entry>
    - <entry>
      <id>http://services.odata.org/V3/OData/OData.svc/Products(1)</id> 
      <category term="ODataDemo.Product" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" title="Product" href="Products(1)" /> 
    - <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Categories" type="application/atom+xml;type=feed" title="Categories" href="Products(1)/Categories">
    - <m:inline>
    - <feed>
      <id>http://services.odata.org/V3/OData/OData.svc/Products(1)/Categories</id> 
      <title type="text">Categories</title> 
      <updated>2014-01-07T15:47:23Z</updated> 
      <link rel="self" title="Categories" href="Products(1)/Categories" /> 
    - <entry>
      <id>http://services.odata.org/V3/OData/OData.svc/Categories(0)</id> 
      <category term="ODataDemo.Category" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" title="Category" href="Categories(0)" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed" title="Products" href="Categories(0)/Products" /> 
      <title type="text">Food</title> 
      <updated>2014-01-07T15:47:23Z</updated> 
    - <author>
      <name /> 
      </author>
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Products" type="application/xml" title="Products" href="Categories(0)/$links/Products" /> 
    - <content type="application/xml">
    - <m:properties>
      <d:ID m:type="Edm.Int32">0</d:ID> 
      <d:Name>Food</d:Name> 
      </m:properties>
      </content>
      </entry>
    - <entry>
      <id>http://services.odata.org/V3/OData/OData.svc/Categories(1)</id> 
      <category term="ODataDemo.Category" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link rel="edit" title="Category" href="Categories(1)" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed" title="Products" href="Categories(1)/Products" /> 
      <title type="text">Beverages</title> 
      <updated>2014-01-07T15:47:23Z</updated> 
    - <author>
      <name /> 
      </author>
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Products" type="application/xml" title="Products" href="Categories(1)/$links/Products" /> 
    - <content type="application/xml">
    - <m:properties>
      <d:ID m:type="Edm.Int32">1</d:ID> 
      <d:Name>Beverages</d:Name> 
      </m:properties>
      </content>
      </entry>
      </feed>
      </m:inline>
      </link>
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Supplier" type="application/atom+xml;type=entry" title="Supplier" href="Products(1)/Supplier" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ProductDetail" type="application/atom+xml;type=entry" title="ProductDetail" href="Products(1)/ProductDetail" /> 
      <title type="text">Milk</title> 
      <summary type="text">Low fat milk</summary> 
      <updated>2014-01-07T15:47:23Z</updated> 
    - <author>
      <name /> 
      </author>
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Categories" type="application/xml" title="Categories" href="Products(1)/$links/Categories" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/Supplier" type="application/xml" title="Supplier" href="Products(1)/$links/Supplier" /> 
      <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/relatedlinks/ProductDetail" type="application/xml" title="ProductDetail" href="Products(1)/$links/ProductDetail" /> 
    - <content type="application/xml">
    - <m:properties>
      <d:ID m:type="Edm.Int32">1</d:ID> 
      <d:ReleaseDate m:type="Edm.DateTimeOffset">1995-10-01T00:00:00Z</d:ReleaseDate> 
      <d:DiscontinuedDate m:null="true" /> 
      <d:Rating m:type="Edm.Int16">3</d:Rating> 
      <d:Price m:type="Edm.Double">3.5</d:Price> 
      </m:properties>
      </content>
      </entry>
    </feed>

1 个答案:

答案 0 :(得分:1)

ID 只是属于the service's schema定义的实体产品的另一个属性。与架构中定义的产品的其他属性(名称描述 ReleaseDate 等)一样,不保证唯一性,除非attribute也被定义为实体的Key。在这种情况下, ID 实际上是产品的密钥,因此我们知道 ID 对于所有产品实体都是唯一的。

在同一架构中,类别实体还具有属性 ID ,该属性用作该实体的密钥。这意味着每个产品在所有产品中都具有唯一的 ID 值,并且每个类别在所有类别中都具有唯一的 ID 值。

为了更好地说明这一点,请在the OData API explorer处尝试此查询以显示属于每个产品的类别:
http://services.odata.org/V3/OData/OData.svc/Products?$expand=Categories