UniqueKeyPolicy Class
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents the unique key policy configuration for specifying uniqueness constraints on documents in the collection in the Azure Cosmos DB service.
public sealed class UniqueKeyPolicy : Microsoft.Azure.Documents.JsonSerializable
type UniqueKeyPolicy = class
inherit JsonSerializable
Public NotInheritable Class UniqueKeyPolicy
Inherits JsonSerializable
- Inheritance
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 collection = await client.CreateDocumentCollectionAsync(databaseLink, collectionSpec });
var doc = JObject.Parse("{"name": { "first": "John", "last": "Smith" }, "alias":"johnsmith" }"); await client.CreateDocumentAsync(collection.SelfLink, doc);
doc = JObject.Parse("{"name": { "first": "James", "last": "Smith" }, "alias":"jamessmith" }"); await client.CreateDocumentAsync(collection.SelfLink, doc);
try { // Error: first+last name is not unique. doc = JObject.Parse("{"name": { "first": "John", "last": "Smith" }, "alias":"johnsmith1" }"); await client.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" }"); await client.CreateDocumentAsync(collection.SelfLink, doc); throw new Exception("CreateDocumentAsync should have thrown exception/conflict"); } catch (DocumentClientException ex) { if (ex.StatusCode != System.Net.HttpStatusCode.Conflict) throw; }
Unique |
Initializes a new instance of the UniqueKeyPolicy class for the Azure Cosmos DB service. |
Unique |
Gets or sets collection of UniqueKey that guarantee uniqueness of documents in collection in the Azure Cosmos DB service. |
Equals(Object) | |
Get |
|
Load |
Loads the object from the specified JSON reader in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
Load |
Loads the object from the specified JSON reader in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
Save |
Saves the object to the specified stream in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
Save |
Saves the object to the specified stream in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
To |
Returns the string representation of the object in the Azure Cosmos DB service. (Inherited from JsonSerializable) |
Produk | Versi |
---|---|
Azure SDK for .NET | Latest |
Azure SDK for .NET maklum balas
Azure SDK for .NET ialah projek sumber terbuka. Pilih pautan untuk memberikan maklum balas: