public async Task when_associated_reference_set_to_null__entity_is_preserved()
        {
            using (TestDbContext context = new TestDbContext())
            {
                IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context);

                // GIVEN an enity root with references:
                AssociatedReference[] references = new[]
                {
                        new AssociatedReference { Id = 1, Name = "Associated Reference 1" },
                        new AssociatedReference { Id = 2, Name = "Associated Reference 2" }
                    };
                context.AddRange(references);
                context.Add(new Entity
                {
                    AssociatedReference = references[0],
                });
                context.SaveChanges();

                // WHEN the owned and the associated references are set to null:
                Entity detachedEntity = new Entity
                {
                    Id = 1,
                    AssociatedReference = null,
                    OwnedReference = null
                };

                await detachedContext.Set<Entity>().UpdateAsync(detachedEntity);
                await detachedContext.SaveChangesAsync();

                // THEN the associated reference still exsits:
                Assert.True(context.AssociatedReferences.Any(a => a.Name == "Associated Reference 1"));
            }
        }
 public async Task when_load_sorted__result_is_ordered()
 {
     using (TestDbContext dbContext = new TestDbContext())
     {
         IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(dbContext);
         dbContext.AddRange(new[]
         {
             new Entity { Name = "Order By Entity 2" },
             new Entity { Name = "Order By Entity 1" },
             new Entity { Name = "Order By Entity 3" }
         });
         await dbContext.SaveChangesAsync();
     }
 }
        public async Task when_entity_deleted__owned_properties_are_deleted()
        {
            using (TestDbContext context = new TestDbContext())
            {
                IDetachedContext<TestDbContext> detachedContext = new DetachedContext<TestDbContext>(context);

                // GIVEN an enity root with an owned list:
                var associatedItems = new List<AssociatedListItem>(new[]
                {
                    new AssociatedListItem { Name = "Associated Item 1" },
                    new AssociatedListItem { Name = "Associated Item 2" }
                });
                context.AddRange(associatedItems);
                await context.SaveChangesAsync();

                context.Add(new Entity
                {
                    OwnedList = new List<OwnedListItem>(new[]  {
                        new OwnedListItem { Name = "Owned Item A" },
                        new OwnedListItem { Name = "Owned Item B" },
                        new OwnedListItem { Name = "Owned Item C" }
                    }),
                    AssociatedList = associatedItems
                });
                context.SaveChanges();

                // WHEN the entity is deleted:
                await detachedContext.Set<Entity>().DeleteAsync(1);
                await detachedContext.SaveChangesAsync();

                // THEN owned items are removed:
                Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item A"));
                Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item B"));
                Assert.False(context.OwnedListItems.Any(e => e.Name == "Owned Item C"));

                // and the associated items are not removed:
                Assert.True(context.AssociatedListItems.Any(e => e.Name == "Associated Item 1"));
                Assert.True(context.AssociatedListItems.Any(e => e.Name == "Associated Item 2"));
            }
        }