Skip to content
Permalink
Browse files

Added "RemoveById" to MongoDbSet

  • Loading branch information...
Turnerj committed May 15, 2019
1 parent 5a31ce7 commit 87fb990c6a6c16dd2c0550958f6ab003808b984f
Showing with 32 additions and 0 deletions.
  1. +9 −0 src/MongoFramework/MongoDbSet.cs
  2. +23 −0 tests/MongoFramework.Tests/MongoDbSetTests.cs
@@ -1,4 +1,5 @@
using MongoFramework.Infrastructure;
using MongoFramework.Infrastructure.Commands;
using MongoFramework.Infrastructure.Indexing;
using MongoFramework.Infrastructure.Linq;
using MongoFramework.Infrastructure.Linq.Processors;
@@ -143,6 +144,14 @@ public virtual void RemoveRange(IEnumerable<TEntity> entities)
ChangeTracker.Update(entity, EntityEntryState.Deleted);
}
}
/// <summary>
/// Stages a deletion for the entity that matches the specified ID
/// </summary>
/// <param name="entityId"></param>
public virtual void RemoveById(object entityId)
{
EntityWriterPipeline.StageCommand(new RemoveEntityByIdCommand<TEntity>(entityId));
}

/// <summary>
/// Writes all of the items in the changeset to the database.
@@ -167,5 +167,28 @@ public void SuccessfullyRemoveRange()
Assert.IsFalse(dbSet.Any(m => m.RequiredField == "SuccessfullyRemoveRange.1"));
Assert.IsFalse(dbSet.Any(m => m.RequiredField == "SuccessfullyRemoveRange.2"));
}

[TestMethod]
public void SuccessfullyRemoveEntityById()
{
var dbSet = new MongoDbSet<MongoDbSetValidationModel>();
dbSet.SetConnection(TestConfiguration.GetConnection());

var entity = new MongoDbSetValidationModel
{
RequiredField = "SuccessfullyRemoveEntityById"
};

dbSet.Add(entity);
dbSet.SaveChanges();

dbSet.SetConnection(TestConfiguration.GetConnection());

dbSet.RemoveById(entity.Id);

Assert.IsTrue(dbSet.Any(m => m.RequiredField == "SuccessfullyRemoveEntityById"));
dbSet.SaveChanges();
Assert.IsFalse(dbSet.Any(m => m.RequiredField == "SuccessfullyRemoveEntityById"));
}
}
}

0 comments on commit 87fb990

Please sign in to comment.
You can’t perform that action at this time.