Table of Contents

Class One<TEntity>

Namespace
MongoDB.Entities
Assembly
MongoDB.Entities.dll

Represents a one-to-one relationship with an IEntity.

public class One<TEntity> where TEntity : IEntity

Type Parameters

TEntity

Any type that implements IEntity

Inheritance
One<TEntity>
Inherited Members

Constructors

One()

public One()

One(string)

Initializes a reference to an entity in MongoDB.

public One(string id)

Parameters

id string

the ID of the referenced entity

One(TEntity)

Initializes a reference to an entity in MongoDB.

public One(TEntity entity)

Parameters

entity TEntity

The actual entity this reference represents.

Properties

ID

The Id of the entity referenced by this instance.

[AsObjectId]
public string ID { get; set; }

Property Value

string

Methods

ToEntityAsync(DB, IClientSessionHandle?, CancellationToken)

Fetches the actual entity this reference represents from the database.

public Task<TEntity> ToEntityAsync(DB db, IClientSessionHandle? session = null, CancellationToken cancellation = default)

Parameters

db DB

The DB instance where these operations will be performed

session IClientSessionHandle

An optional session

cancellation CancellationToken

An optional cancellation token

Returns

Task<TEntity>

A Task containing the actual entity

ToEntityAsync(Func<ProjectionDefinitionBuilder<TEntity>, ProjectionDefinition<TEntity, TEntity>>, DB, IClientSessionHandle?, CancellationToken)

Fetches the actual entity this reference represents from the database with a projection.

public Task<TEntity> ToEntityAsync(Func<ProjectionDefinitionBuilder<TEntity>, ProjectionDefinition<TEntity, TEntity>> projection, DB db, IClientSessionHandle? session = null, CancellationToken cancellation = default)

Parameters

projection Func<ProjectionDefinitionBuilder<TEntity>, ProjectionDefinition<TEntity, TEntity>>

p=> p.Include("Prop1").Exclude("Prop2")

db DB

The DB instance where these operations will be performed

session IClientSessionHandle

An optional session if using within a transaction

cancellation CancellationToken

An optional cancellation token

Returns

Task<TEntity>

A Task containing the actual projected entity

Exceptions

InvalidOperationException

thrown if the entity cannot be found in the database or more than one entity matching the ID is found.

ToEntityAsync(Expression<Func<TEntity, TEntity>>, DB, IClientSessionHandle?, CancellationToken)

Fetches the actual entity this reference represents from the database with a projection.

public Task<TEntity> ToEntityAsync(Expression<Func<TEntity, TEntity>> projection, DB db, IClientSessionHandle? session = null, CancellationToken cancellation = default)

Parameters

projection Expression<Func<TEntity, TEntity>>

x => new Test { PropName = x.Prop }

db DB

The DB instance where these operations will be performed

session IClientSessionHandle

An optional session if using within a transaction

cancellation CancellationToken

An optional cancellation token

Returns

Task<TEntity>

A Task containing the actual projected entity

Exceptions

InvalidOperationException

thrown if the entity cannot be found in the database or more than one entity matching the ID is found.