Ejemplo n.º 1
0
        public void Can_add_update_delete_end_to_end_with_partition_key()
        {
            using (var testDatabase = CosmosTestStore.CreateInitialized(DatabaseName))
            {
                var options = Fixture.CreateOptions(testDatabase);

                var customer = new Customer {
                    Id = 42, Name = "Theon", PartitionKey = 1
                };

                using (var context = new PartitionKeyContext(options))
                {
                    context.Database.EnsureCreated();

                    context.Add(customer);

                    context.SaveChanges();
                }

                using (var context = new PartitionKeyContext(options))
                {
                    var customerFromStore = context.Set <Customer>().Single();

                    Assert.Equal(42, customerFromStore.Id);
                    Assert.Equal("Theon", customerFromStore.Name);
                    Assert.Equal(1, customerFromStore.PartitionKey);

                    customerFromStore.Name = "Theon Greyjoy";

                    context.SaveChanges();
                }

                using (var context = new PartitionKeyContext(options))
                {
                    var customerFromStore = context.Set <Customer>().Single();

                    Assert.Equal(42, customerFromStore.Id);
                    Assert.Equal("Theon Greyjoy", customerFromStore.Name);
                    Assert.Equal(1, customerFromStore.PartitionKey);

                    context.Remove(customerFromStore);

                    context.SaveChanges();
                }

                using (var context = new PartitionKeyContext(options))
                {
                    Assert.Equal(0, context.Set <Customer>().Count());
                }
            }
        }
Ejemplo n.º 2
0
        public async Task Can_add_update_delete_end_to_end_with_partition_key()
        {
            var options = Fixture.CreateOptions();

            var customer = new Customer
            {
                Id           = 42,
                Name         = "Theon",
                PartitionKey = 1
            };

            using (var context = new PartitionKeyContext(options))
            {
                await context.Database.EnsureCreatedAsync();

                context.Add(customer);
                context.Add(
                    new Customer
                {
                    Id           = 42,
                    Name         = "Theon Twin",
                    PartitionKey = 2
                });

                await context.SaveChangesAsync();
            }

            using (var context = new PartitionKeyContext(options))
            {
                var customerFromStore = await context.Set <Customer>().OrderBy(c => c.PartitionKey).FirstAsync();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal("Theon", customerFromStore.Name);
                Assert.Equal(1, customerFromStore.PartitionKey);

                customerFromStore.Name = "Theon Greyjoy";

                await context.SaveChangesAsync();
            }

            using (var context = new PartitionKeyContext(options))
            {
                var customerFromStore = await context.Set <Customer>().OrderBy(c => c.PartitionKey).FirstAsync();

                customerFromStore.PartitionKey = 2;

                Assert.Equal(
                    CoreStrings.KeyReadOnly(nameof(Customer.PartitionKey), nameof(Customer)),
                    Assert.Throws <InvalidOperationException>(() => context.SaveChanges()).Message);
            }

            using (var context = new PartitionKeyContext(options))
            {
                var customerFromStore = await context.Set <Customer>().OrderBy(c => c.PartitionKey).FirstAsync();

                Assert.Equal(42, customerFromStore.Id);
                Assert.Equal("Theon Greyjoy", customerFromStore.Name);
                Assert.Equal(1, customerFromStore.PartitionKey);

                context.Remove(customerFromStore);

                context.Remove(await context.Set <Customer>().OrderBy(c => c.PartitionKey).LastAsync());

                await context.SaveChangesAsync();
            }

            using (var context = new PartitionKeyContext(options))
            {
                Assert.Empty(await context.Set <Customer>().ToListAsync());
            }
        }