public async Task UpdateMEntityAddNewNEntity()
        {
            const string newNEntityName = "UpdateMEntityAddNewNEntity - newNEntity";
            var mEntity = await DatabaseInitializeHelper.CreateSimpleMEntity();

            var newNEntity = new NEntity
            {
                Name = newNEntityName,
                ObjectState = EObjectState.Added
            };

            mEntity.NEntities.Add(newNEntity);
            mEntity.ObjectState = EObjectState.Modified;

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(mEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var loadedMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id), mE => mE.NEntities);
                Assert.AreEqual(1, loadedMEntity.NEntities.Count);
                Assert.AreEqual(newNEntityName, loadedMEntity.NEntities.First().Name);
            }
        }
        public static async Task<MEntity> CreateMEntityWithSomeNEntites()
        {
            var mEntity = new MEntity
            {
                Name = "MEntityWithSomeNEntities " + DateTime.Now,
                ObjectState = EObjectState.Added
            };

            var firstNEntity = new NEntity
            {
                Name = "FirstRelatedNEntity " + DateTime.Now,

                ObjectState = EObjectState.Added
            };

            var secondNEntitiy = new NEntity
            {
                Name = "SecondRelatedNEntity " + DateTime.Now,
                ObjectState = EObjectState.Added
            };

            mEntity.NEntities.Add(firstNEntity);
            mEntity.NEntities.Add(secondNEntitiy);

            using (IDataAccessor dataAccessor = new DataAccessor(DbContextFactory))
            {
                dataAccessor.InsertOrUpdate(mEntity);
                await dataAccessor.SaveChangesAsync();
            }
            return mEntity;
        }
        public async Task DeleteSimpleEntity()
        {
            var mEntity = await DatabaseInitializeHelper.CreateSimpleMEntity();
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                //Sicherstellen, dass die MEntity auch in der DB bekannt ist.
                Assert.IsTrue(await dataAccessor.Set<MEntity>().AnyAsync(mE => mE.Id.Equals(mEntity.Id)));
            }



            //Neuen DataAccessor holen, damit die Operation nicht durch LocalCache verfälscht wird
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.Delete(mEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }


            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                //Sicherstellen, dass die MEntity auch in der DB bekannt ist.
                Assert.IsFalse(await dataAccessor.Set<MEntity>().AnyAsync(mE => mE.Id.Equals(mEntity.Id)));
            }

        }
        public static async Task<NEntity> CreateSimpleNEntity()
        {
            var newNEntity = new NEntity
            {
                Name = "Simple NEntity " + DateTime.Now,
                ObjectState = EObjectState.Added
            };

            using (IDataAccessor dataAccessor = new DataAccessor(DbContextFactory))
            {
                dataAccessor.InsertOrUpdate(newNEntity);
                await dataAccessor.SaveChangesAsync();
            }
            return newNEntity;
        }
        public async Task InsertMEntityWithSomeNEntities()
        {
            const string mEntityName = "InsertMEntityWithSomeNEntities - Parent MEntity";
            const string firstNEntityName = "InsertMEntityWithSomeNEntities - First NEntity";
            const string secondNEntityName = "InsertMEntityWithSomeNEntities - Second NEntity";

            var mEntity = new MEntity
            {
                Name = mEntityName,
                ObjectState = EObjectState.Added
            };

            var firstNEntity = new NEntity
            {
                Name = firstNEntityName,
                ObjectState = EObjectState.Added
            };

            var secondNEntitiy = new NEntity
            {
                Name = secondNEntityName,
                ObjectState = EObjectState.Added
            };

            mEntity.NEntities.Add(firstNEntity);
            mEntity.NEntities.Add(secondNEntitiy);

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(mEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                Assert.IsTrue(await dataAccessor.Set<MEntity>().AnyAsync(mE => mE.Name.EndsWith(mEntityName)));
                var loadedMEntity = await dataAccessor.GetSingleAsync<MEntity>(entity => entity.Name.EndsWith(mEntityName),
                    entity => entity.NEntities);
                Assert.AreEqual(2, loadedMEntity.NEntities.Count);
                Assert.IsTrue(loadedMEntity.NEntities.Any(nE => nE.Name.Equals(secondNEntityName)));
                Assert.IsTrue(loadedMEntity.NEntities.Any(nE => nE.Name.Equals(firstNEntityName)));
            }
        }
Exemple #6
0
        public async Task SetWithAutoIncludedNavigationProperties()
        {
            var mEntity = await DatabaseInitializeHelper.CreateMEntityWithSomeNEntites();
            var nEntity = await DatabaseInitializeHelper.CreateNEntityWithSomeOtherEntities();

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.ModifyRelatedEntities(mEntity, mE => mE.NEntities, EntityState.Added, nEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true after Modifying the Model!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedMEntity = await dataAccessor.Set<MEntity>(mE => mE.NEntities, mE => mE.NEntities.Select(nE => nE.OtherEntities))
                    .SingleAsync(mE => mE.Id.Equals(mEntity.Id));
                Assert.IsTrue(reloadedMEntity.NEntities.Any(nE => nE.OtherEntities.Count > 0));
            }
        }
        public async Task UseNestedDataAccessors()
        {
            var mEntity = await DatabaseInitializeHelper.CreateSimpleMEntity();
            var nEntity = new NEntity
            {
                Name = "UseNestedDataAccessors - new NEntity",
                ObjectState = EObjectState.Added
            };
            var otherEntity = new OtherEntity
            {
                Name = "UseNestedDataAccessors - new OtherEntity",
                ObjectState = EObjectState.Added,
                NEntity = nEntity,
                NEntityId = nEntity.Id
            };

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(nEntity);
                dataAccessor.ModifyRelatedEntities(mEntity, mE => mE.NEntities, EntityState.Added, nEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                using (IDataAccessor secondDataAccessor = new DataAccessor((DataAccessorBase)dataAccessor))
                {
                    secondDataAccessor.InsertOrUpdate(otherEntity);
                    Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                    await secondDataAccessor.SaveChangesAsync();
                    Assert.IsFalse(secondDataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
                }
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedMEntity =
                    await
                        dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id), mE => mE.NEntities,
                            mE => mE.NEntities.Select(nE => nE.OtherEntities));
                Assert.IsTrue(reloadedMEntity.NEntities.Any(nE => nE.Id.Equals(nEntity.Id)));
                Assert.AreEqual(1, reloadedMEntity.NEntities.Count);
                Assert.IsTrue(reloadedMEntity.NEntities.First().OtherEntities.Any(oE => oE.Id.Equals(otherEntity.Id)));
            }
        }
        public async Task DeleteRelatedEntity()
        {
            var mEntity = await DatabaseInitializeHelper.CreateMEntityWithSomeNEntites();
            var deletingNEntity = mEntity.NEntities.First();

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.Delete(deletingNEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedMEntity =
                    await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id), mE => mE.NEntities);
                Assert.AreEqual(1, reloadedMEntity.NEntities.Count);
                Assert.IsFalse(await dataAccessor.Set<MEntity>().AnyAsync(nE => nE.Id.Equals(deletingNEntity.Id)));
            }
        }
        public async Task UpdateSimpleEntity()
        {
            const string updatedMEntityName = "UpdateSimpleEntity - updatedName";
            var mEntity = await DatabaseInitializeHelper.CreateSimpleMEntity();

            mEntity.Name = updatedMEntityName;
            mEntity.ObjectState = EObjectState.Modified;

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(mEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                Assert.IsTrue((await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id))).Name.Equals(updatedMEntityName));
            }
        }
        public async Task InsertSingleEntity()
        {
            const string newEntityName = "InsertSingleEntityTest - First MEntity";
            var newMEntity = new MEntity
            {
                Name = newEntityName,
                ObjectState = EObjectState.Added
            };

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(newMEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                Assert.IsTrue(await dataAccessor.Set<MEntity>().AnyAsync(mE => mE.Name == newEntityName));
            }
        }
        public async Task InsertNEntityWithSomeOtherEntities()
        {
            const string nEntityName = "InsertNEntityWithOtherEntities - NEntity";
            const string firstOtherEntityName = "InsertNEntityWithOtherEntities - FirstOtherEntity";
            const string secondOtherEntityName = "InsertNEntityWithOtherEntities - SecondOtherEntity";



            var nEntity = new NEntity
            {
                Name = nEntityName,
                ObjectState = EObjectState.Added
            };

            var firstOtherEntity = new OtherEntity
            {
                Name = firstOtherEntityName,
                ObjectState = EObjectState.Added
            };

            var secondOtherEntity = new OtherEntity
            {
                Name = secondOtherEntityName,
                ObjectState = EObjectState.Added
            };

            nEntity.OtherEntities.Add(firstOtherEntity);
            nEntity.OtherEntities.Add(secondOtherEntity);

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(nEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                Assert.IsTrue(await dataAccessor.Set<NEntity>().AnyAsync(nE => nE.Name.Equals(nEntityName)));
                var loadedNEntity = await dataAccessor.GetSingleAsync<NEntity>(nE => nE.Name.Equals(nEntityName), entity => entity.OtherEntities);
                Assert.AreEqual(2, loadedNEntity.OtherEntities.Count);
                Assert.IsTrue(loadedNEntity.OtherEntities.Any(oE => oE.Name.Equals(firstOtherEntityName)));
                Assert.IsTrue(loadedNEntity.OtherEntities.Any(oE => oE.Name.Equals(secondOtherEntityName)));
            }

        }
        public async Task UpdateMEntityAddExistingNEntity()
        {
            var mEntity = await DatabaseInitializeHelper.CreateSimpleMEntity();
            var nEntity = await DatabaseInitializeHelper.CreateSimpleNEntity();

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.ModifyRelatedEntities(mEntity, mE => mE.NEntities, EntityState.Added, nEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var loadedMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id), mE => mE.NEntities);
                Assert.AreEqual(nEntity.Id,loadedMEntity.NEntities.First().Id);
            }
        }
        public async Task MultipleRelationModificationsBeforeSave()
        {
            const string newNEntityName = "MultipleRelationModificationsBeforeSave - newNEntity";
            var mEntity = await DatabaseInitializeHelper.CreateMEntityWithSomeNEntites();
            var existingNEntity = await DatabaseInitializeHelper.CreateNEntityWithSomeOtherEntities();
            var newNEntity = new NEntity
            {
                Name = newNEntityName,
                ObjectState = EObjectState.Added
            };

            //Add both Entities to the ObjectList in the Domainmodel
            mEntity.NEntities.Add(newNEntity);
            mEntity.ObjectState = EObjectState.Modified;

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(mEntity);
                dataAccessor.ModifyRelatedEntities(mEntity, mE => mE.NEntities, EntityState.Added, existingNEntity);
                dataAccessor.ModifyRelatedEntities(mEntity, mE => mE.NEntities, EntityState.Deleted, existingNEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id), mE => mE.NEntities);
                var reloadedNEntity = await dataAccessor.GetSingleAsync<NEntity>(nE => nE.Id.Equals(existingNEntity.Id), nE => nE.MEntities);
                Assert.AreEqual(3, reloadedMEntity.NEntities.Count);
                Assert.AreEqual(0, reloadedNEntity.MEntities.Count);
            }
        }
        public async Task UpdateRelatedEntityThroughParent()
        {
            const string updatedNEntityName = "updatedNEntityName";

            var mEntity = await DatabaseInitializeHelper.CreateMEntityWithSomeNEntites();
            var updatingNEntity = mEntity.NEntities.First();

            updatingNEntity.Name = updatedNEntityName;
            updatingNEntity.ObjectState = EObjectState.Modified;

            //Update through parent requires to set the parent Objectstate as "Modified"
            mEntity.ObjectState = EObjectState.Modified;

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                //Just passing the parent to the dataAccessor will do the Job
                dataAccessor.InsertOrUpdate(mEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var loadedMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id), mE => mE.NEntities);
                Assert.AreEqual(updatedNEntityName, loadedMEntity.NEntities.Single(nE => nE.Id.Equals(updatingNEntity.Id)).Name);
            }
        }
        public async Task UpdateOtherEntityAttachToDifferentExistingNEntity()
        {
            var targetNEntity = await DatabaseInitializeHelper.CreateSimpleNEntity();
            var sourceNEntity = await DatabaseInitializeHelper.CreateNEntityWithSomeOtherEntities();

            var switchtingOtherEntity = sourceNEntity.OtherEntities.First();
            switchtingOtherEntity.NEntity = targetNEntity;
            switchtingOtherEntity.NEntityId = targetNEntity.Id;
            switchtingOtherEntity.ObjectState = EObjectState.Modified;


            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(switchtingOtherEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedSourceEntity =
                    await dataAccessor.GetSingleAsync<NEntity>(nE => nE.Id.Equals(sourceNEntity.Id), nE => nE.OtherEntities);
                var reloadedTargetEntity =
                    await dataAccessor.GetSingleAsync<NEntity>(nE => nE.Id.Equals(targetNEntity.Id), nE => nE.OtherEntities);

                Assert.IsFalse(reloadedSourceEntity.OtherEntities.Any(oE => oE.Id.Equals(switchtingOtherEntity.Id)));
                Assert.AreEqual(1, reloadedSourceEntity.OtherEntities.Count);

                Assert.IsTrue(reloadedTargetEntity.OtherEntities.Any(oE => oE.Id.Equals(switchtingOtherEntity.Id)));
                Assert.AreEqual(1, reloadedTargetEntity.OtherEntities.Count);
            }

        }
        public async Task UpdateMEntityRemoveMultiUsedNEntity()
        {
            var firstMEntity = await DatabaseInitializeHelper.CreateMEntityWithSomeNEntites();
            var secondMEntity = await DatabaseInitializeHelper.CreateSimpleMEntity();
            var doubleUsedNEntity = firstMEntity.NEntities.First();

            //NEntity einem zweiten MEntity-Objekt hinzufügen
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.ModifyRelatedEntities(secondMEntity, mE => mE.NEntities, EntityState.Added, doubleUsedNEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }
            //Sicherstellen, dass dies funktioniert hat
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedSecondMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(secondMEntity.Id), mE => mE.NEntities);
                Assert.IsTrue(reloadedSecondMEntity.NEntities.Any(nE => nE.Id.Equals(doubleUsedNEntity.Id)));
            }
            //Entfernen des nun mehrfach verwendeten NEntity-Objekts vom ersten MEntity-Objekt
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.ModifyRelatedEntities(firstMEntity, mE => mE.NEntities, EntityState.Deleted, doubleUsedNEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }
            //Testerfolg prüfen
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedFirstEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(firstMEntity.Id), mE => mE.NEntities);
                Assert.IsFalse(reloadedFirstEntity.NEntities.Any(nE => nE.Id.Equals(doubleUsedNEntity.Id)));
                var reloadedSecondMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(secondMEntity.Id), mE => mE.NEntities);
                Assert.IsTrue(reloadedSecondMEntity.NEntities.Any(nE => nE.Id.Equals(doubleUsedNEntity.Id)));
            }
        }
        public async Task CascadingDeleteNEntityWithOtherEntities()
        {
            var nEntity = await DatabaseInitializeHelper.CreateNEntityWithSomeOtherEntities();

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.Delete(nEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                Assert.IsFalse(await dataAccessor.Set<NEntity>().AnyAsync(nE => nE.Id.Equals(nEntity.Id)));
            }
        }
        public static async Task<NEntity> CreateNEntityWithSomeOtherEntities()
        {
            var nEntity = new NEntity
            {
                Name = "NEntityWithOtherEntity",
                ObjectState = EObjectState.Added
            };

            var firstOtherEntity = new OtherEntity
            {
                Name = "FirstOtherEntity",
                ObjectState = EObjectState.Added
            };

            var secondOtherEntity = new OtherEntity
            {
                Name = "SecondOtherEntity",
                ObjectState = EObjectState.Added
            };

            nEntity.OtherEntities.Add(firstOtherEntity);
            nEntity.OtherEntities.Add(secondOtherEntity);

            using (IDataAccessor dataAccessor = new DataAccessor(DbContextFactory))
            {
                dataAccessor.InsertOrUpdate(nEntity);
                await dataAccessor.SaveChangesAsync();
            }

            return nEntity;
        }
        public async Task InsertMEntityWithSomeNEntitiesWithSomeOtherEntities()
        {
            const string mEntitiyName = "InsertMEntityWithSomeNEntitiesWithSomeOtherEntities - MEntity";

            const string firstNEntityName = "InsertMEntityWithSomeNEntitiesWithSomeOtherEntities - firstNEntity";
            const string secondNEntityName = "InsertMEntityWithSomeNEntitiesWithSomeOtherEntities - secondNEntity";

            const string fNEfirstOtherEntityName =
                "InsertMEntityWithSomeNEntitiesWithSomeOtherEntities - fNEfirstOtherEntityName";
            const string fNEsecondOtherEntityName =
                "InsertMEntityWithSomeNEntitiesWithSomeOtherEntities - fNEsecondOtherEntityName";
            const string sNefirstOtherEntityName =
                "InsertMEntityWithSomeNEntitiesWithSomeOtherEntities - sNEfirstOtherEntityName";

            var mEntity = new MEntity
            {
                Name = mEntitiyName,
                ObjectState = EObjectState.Added,
                NEntities = new ObservableCollection<NEntity>
                {
                    new NEntity
                    {
                        Name = firstNEntityName,
                        ObjectState = EObjectState.Added,
                        OtherEntities = new ObservableCollection<OtherEntity>
                        {
                            new OtherEntity
                            {
                                Name = fNEfirstOtherEntityName,
                                ObjectState = EObjectState.Added
                            },
                            new OtherEntity
                            {
                                Name = fNEsecondOtherEntityName,
                                ObjectState = EObjectState.Added
                            }
                        }
                    },
                        new NEntity
                        {
                            Name = secondNEntityName,
                            ObjectState = EObjectState.Added,
                            OtherEntities = new ObservableCollection<OtherEntity>
                            {
                                new OtherEntity
                                {
                                    Name = sNefirstOtherEntityName,
                                    ObjectState = EObjectState.Added
                                }
                            }
                        }
                }
            };

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.InsertOrUpdate(mEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                Assert.IsTrue(await dataAccessor.Set<MEntity>().AnyAsync(mE => mE.Name.Equals(mEntitiyName)));
                var loadedMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Name.Equals(mEntitiyName),
                    mE => mE.NEntities,
                    mE => mE.NEntities.Select(nE => nE.OtherEntities));

                Assert.AreEqual(2, loadedMEntity.NEntities.Count);
                Assert.AreEqual(2, loadedMEntity.NEntities.Single(nE => nE.Name.Equals(firstNEntityName)).OtherEntities.Count);
            }
        }
 public async Task TrySaveInvalidEntity()
 {
     var otherEntity = new OtherEntity
     {
         Name = "InsertMEntityWithSomeNEntitiesWithSomeOtherEntities - otherEntity without navigation property",
         ObjectState = EObjectState.Added
     };
     using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
     {
         dataAccessor.InsertOrUpdate(otherEntity);
         Assert.Fail("Bei InsertOrUpdate sollte eine Validationexception geflogen sein!");
         await dataAccessor.SaveChangesAsync();
     }
 }
        public async Task DeleteOtherEntity()
        {
            var nEntity = await DatabaseInitializeHelper.CreateNEntityWithSomeOtherEntities();
            var deletingOtherEntity = nEntity.OtherEntities.First();

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                dataAccessor.Delete(deletingOtherEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }
            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var reloadedNEntity =
                    await dataAccessor.GetSingleAsync<NEntity>(nE => nE.Id.Equals(nEntity.Id), nE => nE.OtherEntities);
                Assert.IsFalse(reloadedNEntity.OtherEntities.Any(oE => oE.Id.Equals(deletingOtherEntity.Id)));
            }
        }
        public async Task UpdateRelatedEntityDirectly()
        {
            const string updatedNEntityName = "updatedNEntityName";

            var mEntity = await DatabaseInitializeHelper.CreateMEntityWithSomeNEntites();
            var updatingNEntity = mEntity.NEntities.First();

            updatingNEntity.Name = updatedNEntityName;
            updatingNEntity.ObjectState = EObjectState.Modified;

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                //Just passing the child to the dataAccessor might give us some performance improvement
                dataAccessor.InsertOrUpdate(updatingNEntity);
                Assert.IsTrue(dataAccessor.HasPendingChanges, "HasPendingChanges should be true!");
                await dataAccessor.SaveChangesAsync();
                Assert.IsFalse(dataAccessor.HasPendingChanges, "HasPendingChanges should be false directly after Saving!");
            }

            using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory))
            {
                var loadedMEntity = await dataAccessor.GetSingleAsync<MEntity>(mE => mE.Id.Equals(mEntity.Id), mE => mE.NEntities);
                Assert.AreEqual(updatedNEntityName, loadedMEntity.NEntities.Single(nE => nE.Id.Equals(updatingNEntity.Id)).Name);
            }
        }