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 static async Task<MEntity> CreateSimpleMEntity() { var newMEntity = new MEntity { Name = "Simple MEntity " + DateTime.Now, ObjectState = EObjectState.Added }; using (IDataAccessor dataAccesor = new DataAccessor(DbContextFactory)) { dataAccesor.InsertOrUpdate(newMEntity); await dataAccesor.SaveChangesAsync(); } return newMEntity; }
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))); } }
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 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); } }