public static async Task CreateDocumentIfNotExists(string databaseName, string collectionName,
                                                           DocumentDbEntity entity)
        {
            DocumentClient client;

            // Create a new instance of the DocumentClient
            client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);

            try
            {
                await
                client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseName, collectionName, entity.Id));

                //this.WriteToConsoleAndPromptToContinue("Found {0}", family.Id);
            }
            catch (DocumentClientException de)
            {
                if (de.StatusCode == HttpStatusCode.NotFound)
                {
                    await
                    client.CreateDocumentAsync(
                        UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), entity);

                    //this.WriteToConsoleAndPromptToContinue("Created Family {0}", family.Id);
                }
                else
                {
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        public static async Task <bool> ReplaceDocument(string databaseName, string collectionName, string documentId, DocumentDbEntity entity)
        {
            try
            {
                DocumentClient client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);
                await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseName, collectionName, documentId), entity);

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }