Class DBContext
This db context class can be used as an alternative entry point instead of the DB static class.
Inherited Members
Namespace: MongoDB.Entities
Assembly: MongoDB.Entities.dll
Syntax
public class DBContextConstructors
DBContext(ModifiedBy)
Instantiates a DBContext instance
TIP: will throw an error if no connections have been initialized
Declaration
public DBContext(ModifiedBy modifiedBy = null)Parameters
| Type | Name | Description | 
|---|---|---|
| ModifiedBy | modifiedBy | An optional ModifiedBy instance. When supplied, all save/update operations performed via this DBContext instance will set the value on entities that has a property of type ModifiedBy. You can even inherit from the ModifiedBy class and add your own properties to it. Only one ModifiedBy property is allowed on a single entity type. | 
DBContext(String, MongoClientSettings, ModifiedBy)
Initializes a DBContext instance with the given connection parameters.
TIP: network connection is deferred until the first actual operation.
Declaration
public DBContext(string database, MongoClientSettings settings, ModifiedBy modifiedBy = null)Parameters
| Type | Name | Description | 
|---|---|---|
| String | database | Name of the database | 
| MongoClientSettings | settings | A MongoClientSettings object | 
| ModifiedBy | modifiedBy | An optional ModifiedBy instance. When supplied, all save/update operations performed via this DBContext instance will set the value on entities that has a property of type ModifiedBy. You can even inherit from the ModifiedBy class and add your own properties to it. Only one ModifiedBy property is allowed on a single entity type. | 
DBContext(String, String, Int32, ModifiedBy)
Initializes a DBContext instance with the given connection parameters.
TIP: network connection is deferred until the first actual operation.
Declaration
public DBContext(string database, string host = "127.0.0.1", int port = 27017, ModifiedBy modifiedBy = null)Parameters
| Type | Name | Description | 
|---|---|---|
| String | database | Name of the database | 
| String | host | Address of the MongoDB server | 
| Int32 | port | Port number of the server | 
| ModifiedBy | modifiedBy | An optional ModifiedBy instance. When supplied, all save/update operations performed via this DBContext instance will set the value on entities that has a property of type ModifiedBy. You can even inherit from the ModifiedBy class and add your own properties to it. Only one ModifiedBy property is allowed on a single entity type. | 
Properties
ModifiedBy
The value of this property will be automatically set on entities when saving/updating if the entity has a ModifiedBy property
Declaration
public ModifiedBy ModifiedBy { get; set; }Property Value
| Type | Description | 
|---|---|
| ModifiedBy | 
Session
Returns the session object used for transactions
Declaration
public IClientSessionHandle Session { get; protected set; }Property Value
| Type | Description | 
|---|---|
| IClientSessionHandle | 
Methods
AbortAsync(CancellationToken)
Aborts and rolls back a transaction
Declaration
public Task AbortAsync(CancellationToken cancellation = default(CancellationToken))Parameters
| Type | Name | Description | 
|---|---|---|
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task | 
CommitAsync(CancellationToken)
Commits a transaction to MongoDB
Declaration
public Task CommitAsync(CancellationToken cancellation = default(CancellationToken))Parameters
| Type | Name | Description | 
|---|---|---|
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task | 
CountAsync<T>(FilterDefinition<T>, CancellationToken, CountOptions, Boolean)
Gets an accurate count of how many total entities are in the collection for a given entity type
Declaration
public Task<long> CountAsync<T>(FilterDefinition<T> filter, CancellationToken cancellation = default(CancellationToken), CountOptions options = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| FilterDefinition<T> | filter | A filter definition | 
| CancellationToken | cancellation | An optional cancellation token | 
| CountOptions | options | An optional CountOptions object | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<Int64> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The entity type to get the count for | 
CountAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>, CancellationToken, CountOptions, Boolean)
Gets an accurate count of how many total entities are in the collection for a given entity type
Declaration
public Task<long> CountAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter, CancellationToken cancellation = default(CancellationToken), CountOptions options = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> | filter | f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value) | 
| CancellationToken | cancellation | An optional cancellation token | 
| CountOptions | options | An optional CountOptions object | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<Int64> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The entity type to get the count for | 
CountAsync<T>(Expression<Func<T, Boolean>>, CancellationToken, CountOptions, Boolean)
Gets an accurate count of how many entities are matched for a given expression/filter
Declaration
public Task<long> CountAsync<T>(Expression<Func<T, bool>> expression, CancellationToken cancellation = default(CancellationToken), CountOptions options = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Expression<Func<T, Boolean>> | expression | A lambda expression for getting the count for a subset of the data | 
| CancellationToken | cancellation | An optional cancellation token | 
| CountOptions | options | An optional CountOptions object | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<Int64> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The entity type to get the count for | 
CountAsync<T>(CancellationToken)
Gets an accurate count of how many total entities are in the collection for a given entity type
Declaration
public Task<long> CountAsync<T>(CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<Int64> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The entity type to get the count for | 
CountEstimatedAsync<T>(CancellationToken)
Gets a fast estimation of how many documents are in the collection using metadata.
HINT: The estimation may not be exactly accurate.
Declaration
public Task<long> CountEstimatedAsync<T>(CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<Int64> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The entity type to get the count for | 
CreateCollectionAsync<T>(Action<CreateCollectionOptions<T>>, CancellationToken)
Creates a collection for an Entity type explicitly using the given options
Declaration
public Task CreateCollectionAsync<T>(Action<CreateCollectionOptions<T>> options, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Action<CreateCollectionOptions<T>> | options | The options to use for collection creation | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity that will be stored in the created collection | 
DeleteAsync<T>(FilterDefinition<T>, CancellationToken, Collation, Boolean)
Deletes matching entities with a filter definition
HINT: If the expression matches more than 100,000 entities, they will be deleted in batches of 100k.
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public Task<DeleteResult> DeleteAsync<T>(FilterDefinition<T> filter, CancellationToken cancellation = default(CancellationToken), Collation collation = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| FilterDefinition<T> | filter | A filter definition for matching entities to delete. | 
| CancellationToken | cancellation | An optional cancellation token | 
| Collation | collation | An optional collation object | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<DeleteResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
DeleteAsync<T>(IEnumerable<String>, CancellationToken, Boolean)
Deletes matching entities from MongoDB
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
TIP: Try to keep the number of entities to delete under 100 in a single call
Declaration
public Task<DeleteResult> DeleteAsync<T>(IEnumerable<string> IDs, CancellationToken cancellation = default(CancellationToken), bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| IEnumerable<String> | IDs | An IEnumerable of entity IDs | 
| CancellationToken | cancellation | An optional cancellation token | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<DeleteResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
DeleteAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>, CancellationToken, Collation, Boolean)
Deletes matching entities with a filter expression
HINT: If the expression matches more than 100,000 entities, they will be deleted in batches of 100k.
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public Task<DeleteResult> DeleteAsync<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter, CancellationToken cancellation = default(CancellationToken), Collation collation = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> | filter | f => f.Eq(x => x.Prop, Value) & f.Gt(x => x.Prop, Value) | 
| CancellationToken | cancellation | An optional cancellation token | 
| Collation | collation | An optional collation object | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<DeleteResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
DeleteAsync<T>(Expression<Func<T, Boolean>>, CancellationToken, Collation, Boolean)
Deletes matching entities from MongoDB
HINT: If these entities are referenced by one-to-many/many-to-many relationships, those references are also deleted.
TIP: Try to keep the number of entities to delete under 100 in a single call
Declaration
public Task<DeleteResult> DeleteAsync<T>(Expression<Func<T, bool>> expression, CancellationToken cancellation = default(CancellationToken), Collation collation = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Expression<Func<T, Boolean>> | expression | A lambda expression for matching entities to delete. | 
| CancellationToken | cancellation | An optional cancellation token | 
| Collation | collation | An optional collation object | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<DeleteResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
DeleteAsync<T>(String, CancellationToken, Boolean)
Deletes a single entity from MongoDB
HINT: If this entity is referenced by one-to-many/many-to-many relationships, those references are also deleted.
Declaration
public Task<DeleteResult> DeleteAsync<T>(string ID, CancellationToken cancellation = default(CancellationToken), bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| String | ID | The Id of the entity to delete | 
| CancellationToken | cancellation | An optional cancellation token | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<DeleteResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
Distinct<T, TProperty>()
Represents a MongoDB Distinct command where you can get back distinct values for a given property of a given Entity
Declaration
public Distinct<T, TProperty> Distinct<T, TProperty>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Distinct<T, TProperty> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any Entity that implements IEntity interface | 
| TProperty | The type of the property of the entity you'd like to get unique values for | 
DropCollectionAsync<T>()
Deletes the collection of a given entity type as well as the join collections for that entity.
TIP: When deleting a collection, all relationships associated with that entity type is also deleted.
Declaration
public Task DropCollectionAsync<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Task | 
Type Parameters
| Name | Description | 
|---|---|
| T | The entity type to drop the collection of | 
Find<T>()
Starts a find command for the given entity type
Declaration
public Find<T> Find<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Find<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
Find<T, TProjection>()
Starts a find command with projection support for the given entity type
Declaration
public Find<T, TProjection> Find<T, TProjection>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Find<T, TProjection> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
| TProjection | The type of the end result | 
Fluent<T>(AggregateOptions, Boolean)
Exposes the MongoDB collection for the given entity type as IAggregateFluent in order to facilitate Fluent queries
Declaration
public IAggregateFluent<T> Fluent<T>(AggregateOptions options = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| AggregateOptions | options | The options for the aggregation. This is not required. | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| IAggregateFluent<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
FluentTextSearch<T>(Search, String, Boolean, Boolean, String, AggregateOptions, Boolean)
Start a fluent aggregation pipeline with a $text stage with the supplied parameters
TIP: Make sure to define a text index with DB.Index<T>() before searching
Declaration
public IAggregateFluent<T> FluentTextSearch<T>(Search searchType, string searchTerm, bool caseSensitive = false, bool diacriticSensitive = false, string language = null, AggregateOptions options = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Search | searchType | The type of text matching to do | 
| String | searchTerm | The search term | 
| Boolean | caseSensitive | Case sensitivity of the search (optional) | 
| Boolean | diacriticSensitive | Diacritic sensitivity of the search (optional) | 
| String | language | The language for the search (optional) | 
| AggregateOptions | options | Options for finding documents (not required) | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| IAggregateFluent<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | 
GeoNear<T>(Coordinates2D, Expression<Func<T, Object>>, Boolean, Nullable<Int32>, Nullable<Int32>, Nullable<Int32>, BsonDocument, Nullable<Int32>, Expression<Func<T, Object>>, String, AggregateOptions, Boolean)
Start a fluent aggregation pipeline with a $GeoNear stage with the supplied parameters
Declaration
public IAggregateFluent<T> GeoNear<T>(Coordinates2D NearCoordinates, Expression<Func<T, object>> DistanceField, bool Spherical = true, int? MaxDistance = null, int? MinDistance = null, int? Limit = null, BsonDocument Query = null, int? DistanceMultiplier = null, Expression<Func<T, object>> IncludeLocations = null, string IndexKey = null, AggregateOptions options = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Coordinates2D | NearCoordinates | The coordinates from which to find documents from | 
| Expression<Func<T, Object>> | DistanceField | x => x.Distance | 
| Boolean | Spherical | Calculate distances using spherical geometry or not | 
| Nullable<Int32> | MaxDistance | The maximum distance in meters from the center point that the documents can be | 
| Nullable<Int32> | MinDistance | The minimum distance in meters from the center point that the documents can be | 
| Nullable<Int32> | Limit | The maximum number of documents to return | 
| BsonDocument | Query | Limits the results to the documents that match the query | 
| Nullable<Int32> | DistanceMultiplier | The factor to multiply all distances returned by the query | 
| Expression<Func<T, Object>> | IncludeLocations | Specify the output field to store the point used to calculate the distance | 
| String | IndexKey | |
| AggregateOptions | options | The options for the aggregation. This is not required. | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| IAggregateFluent<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
InsertAsync<T>(T, CancellationToken)
Saves a complete entity replacing an existing entity or creating a new one if it does not exist. If ID value is null, a new entity is created. If ID has a value, then existing entity is replaced.
Declaration
public Task InsertAsync<T>(T entity, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| T | entity | The instance to persist | 
| CancellationToken | cancellation | And optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
InsertAsync<T>(IEnumerable<T>, CancellationToken)
Saves a batch of complete entities replacing an existing entities or creating a new ones if they do not exist. If ID value is null, a new entity is created. If ID has a value, then existing entity is replaced.
Declaration
public Task<BulkWriteResult<T>> InsertAsync<T>(IEnumerable<T> entities, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| IEnumerable<T> | entities | The entities to persist | 
| CancellationToken | cancellation | And optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<BulkWriteResult<T>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
OnBeforeSave<T>()
This event hook will be trigged right before an entity is persisted
Declaration
protected virtual Action<T> OnBeforeSave<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Action<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any entity that implements IEntity | 
OnBeforeUpdate<T>()
This event hook will be triggered right before an update/replace command is executed
Declaration
protected virtual Action<UpdateBase<T>> OnBeforeUpdate<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Action<UpdateBase<T>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any entity that implements IEntity | 
PagedSearch<T>()
Represents an aggregation query that retrieves results with easy paging support.
Declaration
public PagedSearch<T> PagedSearch<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| PagedSearch<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
PagedSearch<T, TProjection>()
Represents an aggregation query that retrieves results with easy paging support.
Declaration
public PagedSearch<T, TProjection> PagedSearch<T, TProjection>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| PagedSearch<T, TProjection> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
| TProjection | The type you'd like to project the results to. | 
PipelineAsync<T, TResult>(Template<T, TResult>, AggregateOptions, CancellationToken, Boolean)
Executes an aggregation pipeline by supplying a 'Template' object. Gets a list back as the result.
Declaration
public Task<List<TResult>> PipelineAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, CancellationToken cancellation = default(CancellationToken), bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Template<T, TResult> | template | A 'Template' object with tags replaced | 
| AggregateOptions | options | The options for the aggregation. This is not required. | 
| CancellationToken | cancellation | An optional cancellation token | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<List<TResult>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
| TResult | The type of the resulting objects | 
PipelineCursorAsync<T, TResult>(Template<T, TResult>, AggregateOptions, CancellationToken, Boolean)
Executes an aggregation pipeline by supplying a 'Template' object. Gets a cursor back as the result.
Declaration
public Task<IAsyncCursor<TResult>> PipelineCursorAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, CancellationToken cancellation = default(CancellationToken), bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Template<T, TResult> | template | A 'Template' object with tags replaced | 
| AggregateOptions | options | The options for the aggregation. This is not required. | 
| CancellationToken | cancellation | An optional cancellation token | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<IAsyncCursor<TResult>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
| TResult | The type of the resulting objects | 
PipelineFirstAsync<T, TResult>(Template<T, TResult>, AggregateOptions, CancellationToken, Boolean)
Executes an aggregation pipeline by supplying a 'Template' object. Gets the first or default value as the result.
Declaration
public Task<TResult> PipelineFirstAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, CancellationToken cancellation = default(CancellationToken), bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Template<T, TResult> | template | A 'Template' object with tags replaced | 
| AggregateOptions | options | The options for the aggregation. This is not required. | 
| CancellationToken | cancellation | An optional cancellation token | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<TResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
| TResult | The type of the resulting objects | 
PipelineSingleAsync<T, TResult>(Template<T, TResult>, AggregateOptions, CancellationToken, Boolean)
Executes an aggregation pipeline by supplying a 'Template' object. Gets a single or default value as the result.
Declaration
public Task<TResult> PipelineSingleAsync<T, TResult>(Template<T, TResult> template, AggregateOptions options = null, CancellationToken cancellation = default(CancellationToken), bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Template<T, TResult> | template | A 'Template' object with tags replaced | 
| AggregateOptions | options | The options for the aggregation. This is not required. | 
| CancellationToken | cancellation | An optional cancellation token | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| Task<TResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
| TResult | The type of the resulting objects | 
Queryable<T>(AggregateOptions, Boolean)
Exposes the MongoDB collection for the given entity type as IQueryable in order to facilitate LINQ queries
Declaration
public IMongoQueryable<T> Queryable<T>(AggregateOptions options = null, bool ignoreGlobalFilters = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| AggregateOptions | options | The aggregate options | 
| Boolean | ignoreGlobalFilters | Set to true if you'd like to ignore any global filters for this operation | 
Returns
| Type | Description | 
|---|---|
| IMongoQueryable<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
Replace<T>()
Starts a replace command for the given entity type
TIP: Only the first matched entity will be replaced
Declaration
public Replace<T> Replace<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Replace<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
SaveAsync<T>(T, CancellationToken)
Saves a complete entity replacing an existing entity or creating a new one if it does not exist. If ID value is null, a new entity is created. If ID has a value, then existing entity is replaced.
Declaration
public Task SaveAsync<T>(T entity, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| T | entity | The instance to persist | 
| CancellationToken | cancellation | And optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
SaveAsync<T>(IEnumerable<T>, CancellationToken)
Saves a batch of complete entities replacing an existing entities or creating a new ones if they do not exist. If ID value is null, a new entity is created. If ID has a value, then existing entity is replaced.
Declaration
public Task<BulkWriteResult<T>> SaveAsync<T>(IEnumerable<T> entities, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| IEnumerable<T> | entities | The entities to persist | 
| CancellationToken | cancellation | And optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<BulkWriteResult<T>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
SaveExceptAsync<T>(T, IEnumerable<String>, CancellationToken)
Saves an entity partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public Task<UpdateResult> SaveExceptAsync<T>(T entity, IEnumerable<string> propNames, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| T | entity | The entity to save | 
| IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<UpdateResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SaveExceptAsync<T>(T, Expression<Func<T, Object>>, CancellationToken)
Saves an entity partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be excluded can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public Task<UpdateResult> SaveExceptAsync<T>(T entity, Expression<Func<T, object>> members, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| T | entity | The entity to save | 
| Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<UpdateResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SaveExceptAsync<T>(IEnumerable<T>, IEnumerable<String>, CancellationToken)
Saves a batch of entities partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public Task<BulkWriteResult<T>> SaveExceptAsync<T>(IEnumerable<T> entities, IEnumerable<string> propNames, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| IEnumerable<T> | entities | The batch of entities to save | 
| IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<BulkWriteResult<T>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SaveExceptAsync<T>(IEnumerable<T>, Expression<Func<T, Object>>, CancellationToken)
Saves a batch of entities partially excluding the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be excluded can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public Task<BulkWriteResult<T>> SaveExceptAsync<T>(IEnumerable<T> entities, Expression<Func<T, object>> members, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| IEnumerable<T> | entities | The batch of entities to save | 
| Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<BulkWriteResult<T>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SaveOnlyAsync<T>(T, IEnumerable<String>, CancellationToken)
Saves an entity partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public Task<UpdateResult> SaveOnlyAsync<T>(T entity, IEnumerable<string> propNames, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| T | entity | The entity to save | 
| IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<UpdateResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SaveOnlyAsync<T>(T, Expression<Func<T, Object>>, CancellationToken)
Saves an entity partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public Task<UpdateResult> SaveOnlyAsync<T>(T entity, Expression<Func<T, object>> members, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| T | entity | The entity to save | 
| Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<UpdateResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SaveOnlyAsync<T>(IEnumerable<T>, IEnumerable<String>, CancellationToken)
Saves a batch of entities partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with an IEnumerable. Property names must match exactly.
Declaration
public Task<BulkWriteResult<T>> SaveOnlyAsync<T>(IEnumerable<T> entities, IEnumerable<string> propNames, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| IEnumerable<T> | entities | The batch of entities to save | 
| IEnumerable<String> | propNames | new List { "PropOne", "PropTwo" } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<BulkWriteResult<T>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SaveOnlyAsync<T>(IEnumerable<T>, Expression<Func<T, Object>>, CancellationToken)
Saves a batch of entities partially with only the specified subset of properties. If ID value is null, a new entity is created. If ID has a value, then existing entity is updated.
TIP: The properties to be saved can be specified with a 'New' expression. You can only specify root level properties with the expression.
Declaration
public Task<BulkWriteResult<T>> SaveOnlyAsync<T>(IEnumerable<T> entities, Expression<Func<T, object>> members, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| IEnumerable<T> | entities | The batch of entities to save | 
| Expression<Func<T, Object>> | members | x => new { x.PropOne, x.PropTwo } | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<BulkWriteResult<T>> | 
Type Parameters
| Name | Description | 
|---|---|
| T | Any class that implements IEntity | 
SavePreservingAsync<T>(T, CancellationToken)
Saves an entity partially while excluding some properties The properties to be excluded can be specified using the [Preserve] or [DontPreserve] attributes.
Declaration
public Task<UpdateResult> SavePreservingAsync<T>(T entity, CancellationToken cancellation = default(CancellationToken))
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| T | entity | The entity to save | 
| CancellationToken | cancellation | An optional cancellation token | 
Returns
| Type | Description | 
|---|---|
| Task<UpdateResult> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
SetGlobalFilter(Type, String, Boolean)
Specify a global filter to be applied to all operations performed with this DBContext
Declaration
protected void SetGlobalFilter(Type type, string jsonString, bool prepend = false)Parameters
| Type | Name | Description | 
|---|---|---|
| Type | type | The type of Entity this global filter should be applied to | 
| String | jsonString | A JSON string filter definition to be applied | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
SetGlobalFilter<T>(FilterDefinition<T>, Boolean)
Specify a global filter to be applied to all operations performed with this DBContext
Declaration
protected void SetGlobalFilter<T>(FilterDefinition<T> filter, bool prepend = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| FilterDefinition<T> | filter | A filter definition to be applied | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of Entity this global filter should be applied to | 
SetGlobalFilter<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>>, Boolean)
Specify a global filter to be applied to all operations performed with this DBContext
Declaration
protected void SetGlobalFilter<T>(Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> filter, bool prepend = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Func<FilterDefinitionBuilder<T>, FilterDefinition<T>> | filter | b => b.Eq(x => x.Prop1, "some value") | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of Entity this global filter should be applied to | 
SetGlobalFilter<T>(Expression<Func<T, Boolean>>, Boolean)
Specify a global filter to be applied to all operations performed with this DBContext
Declaration
protected void SetGlobalFilter<T>(Expression<Func<T, bool>> filter, bool prepend = false)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Expression<Func<T, Boolean>> | filter | x => x.Prop1 == "some value" | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of Entity this global filter should be applied to | 
SetGlobalFilterForBaseClass<TBase>(FilterDefinition<TBase>, Boolean)
Specify a global filter to be applied to all operations performed with this DBContext
Declaration
protected void SetGlobalFilterForBaseClass<TBase>(FilterDefinition<TBase> filter, bool prepend = false)
    where TBase : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| FilterDefinition<TBase> | filter | A filter definition to be applied | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
Type Parameters
| Name | Description | 
|---|---|
| TBase | The type of the base class | 
SetGlobalFilterForBaseClass<TBase>(Func<FilterDefinitionBuilder<TBase>, FilterDefinition<TBase>>, Boolean)
Specify a global filter to be applied to all operations performed with this DBContext
Declaration
protected void SetGlobalFilterForBaseClass<TBase>(Func<FilterDefinitionBuilder<TBase>, FilterDefinition<TBase>> filter, bool prepend = false)
    where TBase : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Func<FilterDefinitionBuilder<TBase>, FilterDefinition<TBase>> | filter | b => b.Eq(x => x.Prop1, "some value") | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
Type Parameters
| Name | Description | 
|---|---|
| TBase | The type of the base class | 
SetGlobalFilterForBaseClass<TBase>(Expression<Func<TBase, Boolean>>, Boolean)
Specify a global filter to be applied to all operations performed with this DBContext
Declaration
protected void SetGlobalFilterForBaseClass<TBase>(Expression<Func<TBase, bool>> filter, bool prepend = false)
    where TBase : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| Expression<Func<TBase, Boolean>> | filter | b => b.Eq(x => x.Prop1, "some value") | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
Type Parameters
| Name | Description | 
|---|---|
| TBase | The type of the base class | 
SetGlobalFilterForInterface<TInterface>(String, Boolean)
Specify a global filter for all entity types that implements a given interface
Declaration
protected void SetGlobalFilterForInterface<TInterface>(string jsonString, bool prepend = false)Parameters
| Type | Name | Description | 
|---|---|---|
| String | jsonString | A JSON string filter definition to be applied | 
| Boolean | prepend | Set to true if you want to prepend this global filter to your operation filters instead of being appended | 
Type Parameters
| Name | Description | 
|---|---|
| TInterface | The interface type to target. Will throw if supplied argument is not an interface type | 
Transaction(String, ClientSessionOptions)
Starts a transaction and returns a session object.
WARNING: Only one transaction is allowed per DBContext instance. Call Session.Dispose() and assign a null to it before calling this method a second time. Trying to start a second transaction for this DBContext instance will throw an exception.
Declaration
public IClientSessionHandle Transaction(string database = null, ClientSessionOptions options = null)Parameters
| Type | Name | Description | 
|---|---|---|
| String | database | The name of the database to use for this transaction. default db is used if not specified | 
| ClientSessionOptions | options | Client session options for this transaction | 
Returns
| Type | Description | 
|---|---|
| IClientSessionHandle | 
Transaction<T>(ClientSessionOptions)
Starts a transaction and returns a session object for a given entity type.
WARNING: Only one transaction is allowed per DBContext instance. Call Session.Dispose() and assign a null to it before calling this method a second time. Trying to start a second transaction for this DBContext instance will throw an exception.
Declaration
public IClientSessionHandle Transaction<T>(ClientSessionOptions options = null)
    where T : IEntityParameters
| Type | Name | Description | 
|---|---|---|
| ClientSessionOptions | options | Client session options (not required) | 
Returns
| Type | Description | 
|---|---|
| IClientSessionHandle | 
Type Parameters
| Name | Description | 
|---|---|
| T | The entity type to determine the database from for the transaction | 
Update<T>()
Starts an update command for the given entity type
Declaration
public Update<T> Update<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| Update<T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
UpdateAndGet<T>()
Starts an update-and-get command for the given entity type
Declaration
public UpdateAndGet<T, T> UpdateAndGet<T>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| UpdateAndGet<T, T> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
UpdateAndGet<T, TProjection>()
Starts an update-and-get command with projection support for the given entity type
Declaration
public UpdateAndGet<T, TProjection> UpdateAndGet<T, TProjection>()
    where T : IEntityReturns
| Type | Description | 
|---|---|
| UpdateAndGet<T, TProjection> | 
Type Parameters
| Name | Description | 
|---|---|
| T | The type of entity | 
| TProjection | The type of the end result |