Compartilhar via


UniqueKeyPolicy Classe

Definição

Representa a configuração de política de chave exclusiva para especificar restrições de exclusividade em documentos na coleção no serviço do 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
Herança
UniqueKeyPolicy
Atributos

Exemplos

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 é exclusivo. new UniqueKey { Paths = new Collection string<> { "/address" } }, } } }; Coleção DocumentCollection = await client. CreateDocumentCollectionAsync(databaseLink, collectionSpec });

var doc = JObject.Parse("{"name": { "first": "John", "last": "Smith" }, "alias":"johnsmith" }"); aguardar cliente. CreateDocumentAsync(collection. SelfLink, doc);

doc = JObject.Parse("{"name": { "first": "James", "last": "Smith" }, "alias":"jamessmith" }"); aguardar cliente. CreateDocumentAsync(collection. SelfLink, doc);

try { // Error: first+last name is not unique. doc = JObject.Parse("{"name": { "first": "John", "last": "Smith" }, "alias":"johnsmith1" }"); aguardar cliente. CreateDocumentAsync(collection. SelfLink, doc); throw new Exception("CreateDocumentAsync should have thrown exception/conflict"); } catch (DocumentClientException ex) { if (ex. StatusCode != System.Net.HttpStatusCode.Conflict) throw; }

try { // Error: alias is not unique. doc = JObject.Parse("{"name": { "first": "James Jr", "last": "Smith" }, "alias":"jamessmith" }"); aguardar cliente. CreateDocumentAsync(collection. SelfLink, doc); throw new Exception("CreateDocumentAsync should have thrown exception/conflict"); } catch (DocumentClientException ex) { if (ex. StatusCode != System.Net.HttpStatusCode.Conflict) throw; }

Construtores

UniqueKeyPolicy()

Representa a configuração de política de chave exclusiva para especificar restrições de exclusividade em documentos na coleção no serviço do Azure Cosmos DB.

Propriedades

UniqueKeys

Obtém a coleção de UniqueKey que garantem a exclusividade dos documentos na coleção no serviço do Azure Cosmos DB.

Aplica-se a