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 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 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 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 UpdateNEntityAddNewOtherEntity() { const string newOtherEntityName = "UpdateNEntityAddNewOtherEntity - NewOtherEntity"; var nEntity = await DatabaseInitializeHelper.CreateSimpleNEntity(); var newOtherEntity = new OtherEntity { Name = newOtherEntityName, ObjectState = EObjectState.Added, NEntityId = nEntity.Id, NEntity = nEntity }; using (IDataAccessor dataAccessor = new DataAccessor(_dbContextFactory)) { dataAccessor.InsertOrUpdate(newOtherEntity); 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 loadedNEntity = await dataAccessor.GetSingleAsync<NEntity>(nE => nE.Id.Equals(nEntity.Id), nE => nE.OtherEntities); Assert.AreEqual(1, loadedNEntity.OtherEntities.Count); Assert.AreEqual(newOtherEntityName, loadedNEntity.OtherEntities.First().Name); } }