Elephant.Database.MongoDb
2.0.0
dotnet add package Elephant.Database.MongoDb --version 2.0.0
NuGet\Install-Package Elephant.Database.MongoDb -Version 2.0.0
<PackageReference Include="Elephant.Database.MongoDb" Version="2.0.0" />
<PackageVersion Include="Elephant.Database.MongoDb" Version="2.0.0" />
<PackageReference Include="Elephant.Database.MongoDb" />
paket add Elephant.Database.MongoDb --version 2.0.0
#r "nuget: Elephant.Database.MongoDb, 2.0.0"
#:package Elephant.Database.MongoDb@2.0.0
#addin nuget:?package=Elephant.Database.MongoDb&version=2.0.0
#tool nuget:?package=Elephant.Database.MongoDb&version=2.0.0
About
Provides lightweight, dependency-injectable helpers for working with MongoDB in .NET projects. It includes a configurable MongoContext, a generic CRUD repository (IGenericCrudRepository), optional assembly-based configuration auto-loading, and utilities for listing databases to help standardize data access and speed up development.
Features:
- MongoDb Configurations (that can optionally be auto-loaded).
- Configurations may optionally be put in a different assembly or even across multiple assemblies.
- Generic CRUD repository.
- Generic MongoContext.
- List all MongoDb databases.
Installation
Choose one:
Package Manager (Visual Studio GUI)
- Right-click your project → "Manage NuGet Packages".
- Search for
Elephant.Database.MongoDb. - Click "Install".
.NET CLI (Command Line)
dotnet add package Elephant.Database.MongoDb
PackageReference (Project File)
<PackageReference Include="Elephant.Database.MongoDb" Version="x.x.x" />
Package Manager (CLI)
nuget install Elephant.Database.MongoDb
How to Use
MongoDb connection strings explained in short
Explanation:
"mongodb://myUser:myPassword@myServerAddress:myMongoDbPortOnServer/myAuthenticationDatabase"
Example with authenication database:
"mongodb://admin:password123@server-001:27017/authDatabase"
Example without authenication database:
"mongodb://admin:password123@server-001:27017"
NOTE: the default MongoDb port is 27017.
You can easily test connection strings to a working MongoDb using Compass (its official and free).
Create a database POCO/Entity
Example:
using Elephant.Database.MongoDb.Types;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDbShop.Entities.Shop
{
/// <summary>
/// Product.
/// </summary>
[BsonIgnoreExtraElements]
public class Product : BaseId // BaseId requires the "Elephant.Database.MongoDb.Types" NuGet.
{
/// <summary>
/// Name.
/// </summary>
[BsonElement("name")]
public string Name { get; set; } = string.Empty;
/// <summary>
/// Quantity in store.
/// </summary>
[BsonElement("quantity")]
public int Quantity { get; set; } = 0;
/// <summary>
/// Price per entity.
/// </summary>
[BsonElement("price")]
public decimal Price { get; set; } = 1.25M;
/// <summary>
/// Description.
/// </summary>
[BsonElement("description")]
public string Description { get; set; } = string.Empty;
}
}
Create a custom repository and interface that inherits from the generic one
Example:
// Requires NuGets "Elephant.Database.MongoDb" and "Elephant.Database.MongoDb.Abstractions".
public interface IProductRepository : IGenericCrudRepository<Product>
{
// Your custom methods here.
}
public class ProductRepository : GenericCrudRepository<Product>, IProductRepository
{
/// <summary>
/// Constructor.
/// </summary>
public ProductRepository(IShopContext shopContext) : base(shopContext.Products)
{
// Your custom methods here. You may use the inherited variable DbSet.
}
}
(Optional) Add them to the dependency injection
public void ConfigureServices(..)
{
..
services.AddMongoContext<IShopContext, ShopContext>(options =>
{
options.ConnectionString = "mongodb://myUser:myPassword@myServerAddress:myServerPort/myAuthenticationDatabase" "Your MongoDb connection string here";
options.DatabaseName = "Your mongoDb Database name here (case sensitive!)";
});
services.AddScoped<IProductRepository, ProductRepository>();
services.AddScoped<IDatabaseRepository, DatabaseRepository>(x => new DatabaseRepository(new MongoClient(Constants.ConnectionString))); // This one is also optional.
}
Conventions
camelCase example
using Elephant.Database.MongoDb;
public void ConfigureServices(IServiceCollection services, ..)
{
ConfigureDatabases(..)
..
}
public void ConfigureDatabases(IServiceCollection services, ..)
{
// Execute this BEFORE creating your MongoDb context. So usually before services.AddMongoContext<..>(options => ..);
ConventionPacks.EnforceGlobalCamelCase(); // Add optional namespaces as needed in the parameter.
// Configure your MongoDB context(s) here somewhere.
}
Upgrade instructions
1.0.0 → 2.0.0
MongoContext.AutoLoadConfigurationsByAssemblyNames(..)now may throw anInvalidOperationExceptioninstead of anNullReferenceException.MongoContextOptionsBuilder.Configure(..),MongoContextOptionsBuilder.GetCollectionInstance(..),MongoContextOptionsBuilder.Entity(..)may throwInvalidOperationExceptionnow instead of aNullReferenceException.Elephant.Database.MongoDb.DbSets.DbSetconstructor changed frompublic DbSet(IMongoContextOptionsBuilder optionsBuilder, IMongoContext context, IMongoCollection<TEntity> collection)intopublic DbSet(IMongoCollection<TEntity> collection)
Contributing
Contributions are welcome. Please read our CONTRIBUTING.md file for guidelines on how to proceed.
License
This project is licensed under the MIT License. See the LICENSE.txt file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Elephant.Database.MongoDb.Abstractions (>= 1.1.3)
- Elephant.Database.MongoDb.Types (>= 1.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Options (>= 10.0.0)
- MongoDB.Driver (>= 2.19.2)
-
net6.0
- Elephant.Database.MongoDb.Abstractions (>= 1.1.3)
- Elephant.Database.MongoDb.Types (>= 1.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Options (>= 6.0.0)
- MongoDB.Driver (>= 2.19.2)
-
net7.0
- Elephant.Database.MongoDb.Abstractions (>= 1.1.3)
- Elephant.Database.MongoDb.Types (>= 1.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
- Microsoft.Extensions.Options (>= 7.0.0)
- MongoDB.Driver (>= 2.19.2)
-
net8.0
- Elephant.Database.MongoDb.Abstractions (>= 1.1.3)
- Elephant.Database.MongoDb.Types (>= 1.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- MongoDB.Driver (>= 2.19.2)
-
net9.0
- Elephant.Database.MongoDb.Abstractions (>= 1.1.3)
- Elephant.Database.MongoDb.Types (>= 1.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- MongoDB.Driver (>= 2.19.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
AutoLoadConfigurationsByAssemblyNames may throw a different error.