public void InsertGraphForMultipleNewAggregateRootEntitesAndChildrenUsingConvention()
        {
            // Arrange
            FakeDbContext dbContext = new FakeDbContext();
            AggregateRootEntityRepository entityRepository = new AggregateRootEntityRepository(dbContext);

            AggregateRootEntity aggregateRootEntity1 = new AggregateRootEntity
            {
                SomeOtherProperty = "Some other property",
                SimpleEntity      = new SimpleEntity {
                    SomeProperty = "Some property"
                },
                OtherSimpleEntities = new List <AnotherSimpleEntity>
                {
                    new AnotherSimpleEntity {
                        SomeProperty = "Child property 1"
                    },
                    new AnotherSimpleEntity {
                        SomeProperty = "Child property 2"
                    },
                }
            };

            AggregateRootEntity aggregateRootEntity2 = new AggregateRootEntity
            {
                SomeOtherProperty = "Some other property",
                SimpleEntity      = new SimpleEntity {
                    SomeProperty = "Some property"
                },
                OtherSimpleEntities = new List <AnotherSimpleEntity>
                {
                    new AnotherSimpleEntity {
                        SomeProperty = "Child property 1"
                    },
                    new AnotherSimpleEntity {
                        SomeProperty = "Child property 2"
                    },
                }
            };

            // Act
            entityRepository.InsertOrUpdateGraph(aggregateRootEntity1);
            entityRepository.InsertOrUpdateGraph(aggregateRootEntity2);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);

            // The AggregateRoot and its children should all be added to the context and all have a state of 'Added'
            Assert.AreEqual(8, entityRepository.DbContext.ChangeTracker.Entries().Count());
            entityRepository.DbContext.ChangeTracker.Entries().ToList().ForEach(entry => Assert.AreEqual(EntityState.Added, entry.State));
        }
        public void InsertGraphForNewAggregateRootEntityAndChildrenWhereChildrenSpecifiedAsUnchanged()
        {
            // Arrange
            const int expectedSimpleId1 = 10;
            const int expectedSimpleId2 = 11;
            const int expectedSimpleId3 = 12;

            FakeDbContext dbContext = new FakeDbContext();
            AggregateRootEntityRepository entityRepository = new AggregateRootEntityRepository(dbContext);

            // Create an aggregate root where one of the 'AnotherSimpleEntity' already exist (i.e. has a primary key)
            AggregateRootEntity aggregateRootEntity = new AggregateRootEntity
            {
                SomeOtherProperty = "Some other property",
                SimpleEntity      = new SimpleEntity {
                    Id = expectedSimpleId1, SomeProperty = "Some property", State = State.Unchanged
                },
                OtherSimpleEntities = new List <AnotherSimpleEntity>
                {
                    new AnotherSimpleEntity {
                        Id = expectedSimpleId2, SomeProperty = "Child property 1", State = State.Unchanged
                    },
                    new AnotherSimpleEntity {
                        Id = expectedSimpleId3, SomeProperty = "Child property 2", State = State.Unchanged
                    },
                }
            };

            // Act
            entityRepository.InsertOrUpdateGraph(aggregateRootEntity);

            // Assert
            Assert.IsNotNull(entityRepository.DbContext);

            // The AggregateRoot and its children should all be added to the context and all have a state of 'Added'
            Assert.AreEqual(4, entityRepository.DbContext.ChangeTracker.Entries().Count());
            entityRepository.DbContext.ChangeTracker.Entries().ToList().ForEach(entry =>
            {
                IEntityWithState <int> entityWithState = (IEntityWithState <int>)entry.Entity;
                Assert.AreEqual(entityWithState.Id == default(int) ? EntityState.Added : EntityState.Unchanged, entry.State);
            });
        }