Exemple #1
0
        private async Task GetStartedDemo()
        {
            this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
            await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseId });

            await this.client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(databaseId), new DocumentCollection { Id = collectionId });

            Family andersenFamily = new Family
            {
                Id       = "Andersen.2",
                LastName = "Andersen",
                Parents  = new Parent[]
                {
                    new Parent {
                        FirstName = "Thomas"
                    },
                    new Parent {
                        FirstName = "Mary Kay"
                    }
                },
                Children = new Child[]
                {
                    new Child
                    {
                        FirstName = "Henriette Thaulow",
                        Gender    = "female",
                        Grade     = 5,
                        Pets      = new Pet[]
                        {
                            new Pet {
                                GivenName = "Fluffy"
                            }
                        }
                    }
                },
                Address = new Address {
                    State = "WA", County = "King", City = "Seattle"
                },
                IsRegistered = true
            };

            await this.CreateFamilyDocumentIfNotExists(databaseId, collectionId, andersenFamily);


            var test = await this.GetDocFromAzureDb(andersenFamily.Id);

            Console.WriteLine(test.Id);
            //Family wakefieldFamily = new Family
            //{
            //    Id = "Wakefield.7",
            //    LastName = "Wakefield",
            //    Parents = new Parent[]
            //        {
            //    new Parent { FamilyName = "Wakefield", FirstName = "Robin" },
            //    new Parent { FamilyName = "Miller", FirstName = "Ben" }
            //        },
            //    Children = new Child[]
            //        {
            //    new Child
            //    {
            //            FamilyName = "Merriam",
            //            FirstName = "Jesse",
            //            Gender = "female",
            //            Grade = 8,
            //            Pets = new Pet[]
            //            {
            //                    new Pet { GivenName = "Goofy" },
            //                    new Pet { GivenName = "Shadow" }
            //            }
            //    },
            //    new Child
            //    {
            //            FamilyName = "Miller",
            //            FirstName = "Lisa",
            //            Gender = "female",
            //            Grade = 1
            //    }
            //        },
            //    Address = new Address { State = "NY", County = "Manhattan", City = "NY" },
            //    IsRegistered = false
            //};

            //await this.CreateFamilyDocumentIfNotExists("FamilyDB", "FamilyCollection", wakefieldFamily);
        }
Exemple #2
0
        private async Task CreateFamilyDocumentIfNotExists(string databaseName, string collectionName, Family family)
        {
            try
            {
                await this.client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, family.Id));

                await this.client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, family.Id), family);

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

                    this.WriteToConsoleAndPromptToContinue("Created Family {0}", family.Id);
                }
                else
                {
                    throw;
                }
            }
        }