Пример #1
0
        private async Task AddItemsToContainerAsyc()
        {
            #region 1st Family
            var andersenFamily = new Family
            {
                Id       = "Andersen.1",
                LastName = "Andersen",
                Parents  = new List <Parent>
                {
                    new Parent {
                        FirstName = "Thomas"
                    },
                    new Parent {
                        FirstName = "Mary Kay"
                    }
                },
                Children = new List <Child>
                {
                    new Child
                    {
                        FirstName = "Henriette Thaulow",
                        Gender    = "female",
                        Grade     = 5,
                        Pets      = new List <Pet>
                        {
                            new Pet {
                                GivenName = "Fluffy"
                            }
                        }
                    }
                },
                Address = new Address {
                    State = "WA", County = "King", City = "Seattle"
                },
                IsRegistered = false
            };

            try
            {
                // Read the item to see if it exists. Exception error will occur if it's not found
                var response = await _container.ReadItemAsync <Family>(andersenFamily.Id, new PartitionKey(andersenFamily.LastName));

                Console.WriteLine("Item in database with id: {0} already exists", response.Resource.Id);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                var response = await _container.CreateItemAsync <Family>(andersenFamily, new PartitionKey(andersenFamily.LastName));

                Console.WriteLine("Item in database with id: {0} created", response.Resource.Id);
                Console.WriteLine("Request charge property: {0}", response.RequestCharge);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"1. AddItemsToContainerAsyc() error: {ex.Message}");
            }
            #endregion

            #region 2nd Family
            var wakefieldFamily = new Family
            {
                Id       = "Wakefield.7",
                LastName = "Wakefield",
                Parents  = new List <Parent>
                {
                    new Parent {
                        FamilyName = "Wakefield", FirstName = "Robin"
                    },
                    new Parent {
                        FamilyName = "Miller", FirstName = "Ben"
                    }
                },
                Children = new List <Child>
                {
                    new Child
                    {
                        FamilyName = "Merriam",
                        FirstName  = "Jesse",
                        Gender     = "female",
                        Grade      = 8,
                        Pets       = new List <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 = true
            };

            try
            {
                var wakefieldFamilyResponse = await _container.ReadItemAsync <Family>(wakefieldFamily.Id, new PartitionKey(wakefieldFamily.LastName));

                Console.WriteLine("Item in database with id: {0} already exists\n", wakefieldFamilyResponse.Resource.Id);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                var response = await _container.CreateItemAsync <Family>(wakefieldFamily, new PartitionKey(wakefieldFamily.LastName));

                Console.WriteLine("Item in database with id: {0} created", response.Resource.Id);
                Console.WriteLine("Request charge property: {0}", response.RequestCharge);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"2. AddItemsToContainerAsyc() error: {ex.Message}");
            }
            #endregion
        }
        private async Task AddItemsToContainerAsync()
        {
            // Create a family object for the Andersen family
            Family andersenFamily = new Family
            {
                Id       = "Andersen.1",
                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 = false
            };

            try
            {
                // Read the item to see if it exists.
                ItemResponse <Family> andersenFamilyResponse = await this.container.ReadItemAsync <Family>(andersenFamily.Id, new PartitionKey(andersenFamily.LastName));

                Console.WriteLine("Item in database with id: {0} already exists\n", andersenFamilyResponse.Resource.Id);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                // Create an item in the container representing the Andersen family. Note we provide the value of the partition key for this item, which is "Andersen"
                ItemResponse <Family> andersenFamilyResponse = await this.container.CreateItemAsync <Family>(andersenFamily, new PartitionKey(andersenFamily.LastName));

                // Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
                Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", andersenFamilyResponse.Resource.Id, andersenFamilyResponse.RequestCharge);
            }

            // Create a family object for the Wakefield family
            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 = true
            };

            try
            {
                // Read the item to see if it exists
                ItemResponse <Family> wakefieldFamilyResponse = await this.container.ReadItemAsync <Family>(wakefieldFamily.Id, new PartitionKey(wakefieldFamily.LastName));

                Console.WriteLine("Item in database with id: {0} already exists\n", wakefieldFamilyResponse.Resource.Id);
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                // Create an item in the container representing the Wakefield family. Note we provide the value of the partition key for this item, which is "Wakefield"
                ItemResponse <Family> wakefieldFamilyResponse = await this.container.CreateItemAsync <Family>(wakefieldFamily, new PartitionKey(wakefieldFamily.LastName));

                // Note that after creating the item, we can access the body of the item with the Resource property off the ItemResponse. We can also access the RequestCharge property to see the amount of RUs consumed on this request.
                Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.\n", wakefieldFamilyResponse.Resource.Id, wakefieldFamilyResponse.RequestCharge);
            }
        }