Exemple #1
0
        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfigurationRoot configuration = builder.Build();

            var cosmosDbService = new CosmosDbService(configuration.GetConnectionString("Uri"), configuration.GetConnectionString("PrimaryKey"));

            cosmosDbService.CreateDb().Wait();

            cosmosDbService.UpdateOrganizationName("Contoso.LTD", "ContosoNewName").Wait();

            Console.ReadKey();
        }
Exemple #2
0
        /// <summary>
        /// Creates a Cosmos DB database and a container with the specified partition key.
        /// </summary>
        /// <returns></returns>
        /// <remarks><see cref="CosmosDbConnectorOptions"/></remarks>
        private static async Task <CosmosDbService> InitializeCosmosClientInstanceAsync(IConfiguration configuration)
        {
            // read settings from "cosmosdb:client" and VCAP:services or services:
            var configMgr  = new ConnectionStringManager(configuration);
            var cosmosInfo = configMgr.Get <CosmosDbConnectionInfo>();

            // these are mapped into the properties dictionary
            var databaseName = cosmosInfo.Properties["DatabaseId"];
            var databaseLink = cosmosInfo.Properties["DatabaseLink"];

            // container is not known to be provided by a service binding:
            var containerName   = configuration.GetValue <string>("CosmosDb:Container");
            var cosmosClient    = new CosmosClient(cosmosInfo.ConnectionString);
            var cosmosDbService = new CosmosDbService(cosmosClient, databaseName, containerName);

            var database = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseName);

            await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");

            return(cosmosDbService);
        }