MadEyeMatt.MongoDB.DbContext
8.1.0
See the version list below for details.
dotnet add package MadEyeMatt.MongoDB.DbContext --version 8.1.0
NuGet\Install-Package MadEyeMatt.MongoDB.DbContext -Version 8.1.0
<PackageReference Include="MadEyeMatt.MongoDB.DbContext" Version="8.1.0" />
paket add MadEyeMatt.MongoDB.DbContext --version 8.1.0
#r "nuget: MadEyeMatt.MongoDB.DbContext, 8.1.0"
// Install MadEyeMatt.MongoDB.DbContext as a Cake Addin #addin nuget:?package=MadEyeMatt.MongoDB.DbContext&version=8.1.0 // Install MadEyeMatt.MongoDB.DbContext as a Cake Tool #tool nuget:?package=MadEyeMatt.MongoDB.DbContext&version=8.1.0
MongoDB.DbContext
A DB context implementation for MongoDB.
The DB context implementation is very heavily inspired by the EntityFramework Core DbContext
infrastructure. The options configuration works the same. The database options can be set when
configuring the context, or agail later (and maybe altered) when new instances of the context
are created.
The context services are registered as scoped automatically. Every context will get the MongoDB
driver services (IMongoDatabase
, IMongoClient
) from an interner service provider which
will have those services registered as singleton instances. This cache of internal service providers
uses the conenction string, str database name and the context options type as key, so changing the
database name for a context at runtime (by overriding OnConfiguring
) will result in a new internal
service provider with the corresponding MongoDB singleton services.
Usage
To add a MongoDbContext
one uses the AddMongoDbContext
extension method. There
are several ways to configure one or multiple contexts.
Configuration without custom context class
To get started quickly, one can simply use the MongoDbContext
class and configure the
database by passing an options builder action to AddMongoDbContext
. This way you have
no possibility to further configure the context at runtime.
services.AddMongoDbContext<MongoDbContext>(builder =>
{
builder.UseDatabase("mongodb://localhost:27017", "test");
});
Configuration with custom context class
If you need more control ove the context configuration or need to add multiple different
contexts, you need to dervice you context class from MongoDbContext
services.AddMongoDbContext<SampleContext>(builder =>
{
builder.UseDatabase("mongodb://localhost:27017", "test");
});
public sealed class SampleContext : MongoDbContext
{
public SampleContext(MongoDbContextOptions options)
: base(options)
{
}
/// <inheritdoc />
protected override void OnConfiguring(MongoDbContextOptionsBuilder builder)
{
builder.UseDatabase("mongodb://localhost:27017", "other");
}
}
The OnConfiguring
method is called everytime a new instance of the context is created,
so it is possible to change the connection settings for the context based on runtime information.
Configuring multiple different context classes
If you need to add several contexts you need to inject the generic version of the MongoDbContextOptions
to allow the correct options be created. There will be an exception thrown if you don't do it.
services.AddMongoDbContext<SampleContextOne>(builder =>
{
builder.UseDatabase("mongodb://localhost:27017", "one");
});
services.AddMongoDbContext<SampleContextTwo>(builder =>
{
builder.UseDatabase("mongodb://localhost:27017", "two");
});
public sealed class SampleContextOne : MongoDbContext
{
public SampleContext(MongoDbContextOptions<SampleContextOne> options)
: base(options)
{
}
}
public sealed class SampleContextTwo : MongoDbContext
{
public SampleContext(MongoDbContextOptions<SampleContextTwo> options)
: base(options)
{
}
}
Configuring telemetry information
To expose telemetry information via System.Diagnostics
just enable it in the MongoDbContextOptions
.
If you like to integrate this telemetry into OpenTelemetry just add a source with the name
MongoDB.Driver.Core.Extensions.DiagnosticSources
.
services.AddMongoDbContext<SampleContext>(builder =>
{
builder
.UseDatabase("mongodb://localhost:27017", "test")
.EnableTelemetry();
});
References
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- JetBrains.Annotations (>= 2023.3.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
- MongoDB.Driver (>= 2.24.0)
- MongoDB.Driver.Core.Extensions.DiagnosticSources (>= 1.4.0)
-
net6.0
- JetBrains.Annotations (>= 2023.3.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
- MongoDB.Driver (>= 2.24.0)
- MongoDB.Driver.Core.Extensions.DiagnosticSources (>= 1.4.0)
-
net7.0
- JetBrains.Annotations (>= 2023.3.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
- MongoDB.Driver (>= 2.24.0)
- MongoDB.Driver.Core.Extensions.DiagnosticSources (>= 1.4.0)
-
net8.0
- JetBrains.Annotations (>= 2023.3.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
- MongoDB.Driver (>= 2.24.0)
- MongoDB.Driver.Core.Extensions.DiagnosticSources (>= 1.4.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on MadEyeMatt.MongoDB.DbContext:
Package | Downloads |
---|---|
Fluxera.Repository.MongoDB
A MongoDB repository implementation. |
|
MadEyeMatt.AspNetCore.Identity.MongoDB
A libary that provides MongoDB UserStore and RoleStore implementations for ASP.NET Identity Core. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
9.0.1 | 39 | 11/14/2024 |
9.0.0 | 35 | 11/14/2024 |
8.1.5 | 182 | 11/1/2024 |
8.1.4 | 1,114 | 7/9/2024 |
8.1.3 | 586 | 5/23/2024 |
8.1.2 | 803 | 4/18/2024 |
8.1.1 | 501 | 3/19/2024 |
8.1.0 | 320 | 2/22/2024 |
8.0.1 | 761 | 1/4/2024 |
8.0.0 | 506 | 11/16/2023 |
7.2.2 | 156 | 11/12/2023 |
7.2.1 | 328 | 7/20/2023 |
7.2.0 | 466 | 4/25/2023 |
7.1.0 | 364 | 4/13/2023 |
7.0.2 | 233 | 4/13/2023 |
7.0.1 | 227 | 4/12/2023 |
7.0.0 | 182 | 4/12/2023 |