Example #1
0
        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)));
            }

        }
Example #2
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));
            }
        }
Example #3
0
        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)));
            }
        }
Example #4
0
        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));
            }
        }
Example #5
0
        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)));
            }
        }
Example #6
0
        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)));
            }

        }
Example #7
0
        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)));
            }
        }
Example #8
0
        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);
            }
        }