예제 #1
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));
            }
        }
예제 #2
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)));
            }
        }
예제 #3
0
        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);
            }
        }
예제 #4
0
        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)));
            }
        }
예제 #5
0
        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);
            }
        }