public void UpdateEntity()
        {
            var database        = TestConfiguration.GetDatabase();
            var entityContainer = new DbEntityCollection <EntityWriterModel>();
            var writer          = new DbEntityWriter <EntityWriterModel>(database);
            var reader          = new DbEntityReader <EntityWriterModel>(database);

            //Get entity initially into the DB so we can update it
            var entity = new EntityWriterModel
            {
                Title = "DbEntityWriterTests.UpdateEntity"
            };

            entityContainer.Update(entity, DbEntityEntryState.Added);
            writer.Write(entityContainer);
            entityContainer.Clear();

            //Our updated entity with the same ID
            var updatedEntity = new EntityWriterModel
            {
                Id    = entity.Id,
                Title = "DbEntityWriterTests.UpdateEntity-Updated"
            };

            entityContainer.Update(updatedEntity, DbEntityEntryState.Updated);
            writer.Write(entityContainer);

            var dbEntity = reader.AsQueryable().Where(e => e.Id == entity.Id).FirstOrDefault();

            Assert.AreEqual("DbEntityWriterTests.UpdateEntity-Updated", dbEntity.Title);
        }
Exemple #2
0
        public void ClearTracker()
        {
            var entityCollection = new DbEntityCollection <EntityCollectionModel>();
            var entity           = new EntityCollectionModel
            {
                Title = "DbEntityCollectionTests.ClearTracker"
            };

            entityCollection.Update(entity, DbEntityEntryState.NoChanges);

            entityCollection.Clear();

            Assert.IsFalse(entityCollection.GetEntries().Any());
        }
        public async Task MixedActionWriteAsync()
        {
            var database        = TestConfiguration.GetDatabase();
            var entityContainer = new DbEntityCollection <EntityWriterModel>();
            var writer          = new DbEntityWriter <EntityWriterModel>(database);
            var reader          = new DbEntityReader <EntityWriterModel>(database);

            var updateEntity = new EntityWriterModel
            {
                Title = "DbEntityWriterTests.MixedActionWriteAsync-UpdateEntity"
            };
            var deleteEntity = new EntityWriterModel
            {
                Title = "DbEntityWriterTests.MixedActionWriteAsync-DeleteEntity"
            };

            entityContainer.Update(updateEntity, DbEntityEntryState.Added);
            entityContainer.Update(deleteEntity, DbEntityEntryState.Added);
            await writer.WriteAsync(entityContainer).ConfigureAwait(false);

            entityContainer.Clear();

            var addedEntity = new EntityWriterModel
            {
                Title = "DbEntityWriterTests.MixedActionWriteAsync-AddEntity"
            };

            updateEntity.Title = "DbEntityWriterTests.MixedActionWriteAsync-UpdateEntity-Updated";
            entityContainer.Update(addedEntity, DbEntityEntryState.Added);
            entityContainer.Update(updateEntity, DbEntityEntryState.Updated);
            entityContainer.Update(deleteEntity, DbEntityEntryState.Deleted);
            await writer.WriteAsync(entityContainer).ConfigureAwait(false);

            Assert.IsTrue(reader.AsQueryable().Where(e => e.Id == addedEntity.Id).Any());
            Assert.IsFalse(reader.AsQueryable().Where(e => e.Id == deleteEntity.Id).Any());

            var dbEntity = reader.AsQueryable().Where(e => e.Id == updateEntity.Id).FirstOrDefault();

            Assert.AreEqual("DbEntityWriterTests.MixedActionWriteAsync-UpdateEntity-Updated", dbEntity.Title);
        }
        public void RemoveEntity()
        {
            var database        = TestConfiguration.GetDatabase();
            var entityContainer = new DbEntityCollection <EntityWriterModel>();
            var writer          = new DbEntityWriter <EntityWriterModel>(database);
            var reader          = new DbEntityReader <EntityWriterModel>(database);

            //Get entity initially into the DB so we can remove it
            var entity = new EntityWriterModel
            {
                Title = "DbEntityWriterTests.RemoveEntity"
            };

            entityContainer.Update(entity, DbEntityEntryState.Added);
            writer.Write(entityContainer);
            entityContainer.Clear();

            //Remove the entity
            entityContainer.Update(entity, DbEntityEntryState.Deleted);
            writer.Write(entityContainer);

            Assert.IsFalse(reader.AsQueryable().Any(e => e.Id == entity.Id));
        }