你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

UniqueKeyPolicy 类

定义

表示用于在 Azure Cosmos DB 服务集合中指定文档的唯一性约束的唯一键策略配置。

[System.Text.Json.Serialization.JsonConverter(typeof(Azure.Cosmos.TextJsonUniqueKeyPolicyConverter))]
public sealed class UniqueKeyPolicy
[<System.Text.Json.Serialization.JsonConverter(typeof(Azure.Cosmos.TextJsonUniqueKeyPolicyConverter))>]
type UniqueKeyPolicy = class
Public NotInheritable Class UniqueKeyPolicy
继承
UniqueKeyPolicy
属性

示例

var collectionSpec = new DocumentCollection { Id = “Collection with unique keys”, UniqueKeyPolicy = new UniqueKeyPolicy { UniqueKeys = new Collection<UniqueKey> { // pair </name/first, name/last> is unique. new UniqueKey { Paths = new Collection<string> { “/name/first”, “/name/last” } }, // /address is unique. new UniqueKey { Paths = new Collection<string> { “/address” } }, } } };DocumentCollection 集合 = await 客户端。CreateDocumentCollectionAsync (databaseLink, collectionSpec }) ;

var doc = JObject.Parse (“{”name“: { ”first“: ”John“, ”last“: ”Smith“ }, ”alias“:”johnsmith“ }”) ;await 客户端。CreateDocumentAsync (集合。SelfLink,doc) ;

doc = JObject.Parse (“{”name“: { ”first“: ”James“, ”last“: ”Smith“ }, ”alias“:”jamessmith“ }”) ;await 客户端。CreateDocumentAsync (集合。SelfLink,doc) ;

try { // 错误:名字+姓氏不唯一。 doc = JObject.Parse (“{”name“: { ”first“: ”John“, ”last“: ”Smith“ }, ”alias“:”johnsmith1“ }”) ;await 客户端。CreateDocumentAsync (集合。SelfLink,doc) ;引发新的异常 (“CreateDocumentAsync 应已引发异常/冲突”) ;} catch (DocumentClientException ex) { if (ex.StatusCode != System.Net.HttpStatusCode.Conflict) 引发;}

try { // 错误: 别名不唯一。 doc = JObject.Parse (“{”name“: { ”first“: ”James Jr“, ”last“: ”Smith“ }, ”alias“:”jamessmith“ }”) ;await 客户端。CreateDocumentAsync (集合。SelfLink,doc) ;引发新的异常 (“CreateDocumentAsync 应已引发异常/冲突”) ;} catch (DocumentClientException ex) { if (ex.StatusCode != System.Net.HttpStatusCode.Conflict) 引发;}

构造函数

UniqueKeyPolicy()

表示用于在 Azure Cosmos DB 服务集合中指定文档的唯一性约束的唯一键策略配置。

属性

UniqueKeys

获取 的 UniqueKey 集合,该集合保证 Azure Cosmos DB 服务集合中文档的唯一性。

适用于