Devorion.MongoDB.SchemaManager 1.0.0-beta.2

This is a prerelease version of Devorion.MongoDB.SchemaManager.
dotnet add package Devorion.MongoDB.SchemaManager --version 1.0.0-beta.2
                    
NuGet\Install-Package Devorion.MongoDB.SchemaManager -Version 1.0.0-beta.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Devorion.MongoDB.SchemaManager" Version="1.0.0-beta.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Devorion.MongoDB.SchemaManager" Version="1.0.0-beta.2" />
                    
Directory.Packages.props
<PackageReference Include="Devorion.MongoDB.SchemaManager" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Devorion.MongoDB.SchemaManager --version 1.0.0-beta.2
                    
#r "nuget: Devorion.MongoDB.SchemaManager, 1.0.0-beta.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Devorion.MongoDB.SchemaManager@1.0.0-beta.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Devorion.MongoDB.SchemaManager&version=1.0.0-beta.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Devorion.MongoDB.SchemaManager&version=1.0.0-beta.2&prerelease
                    
Install as a Cake Tool

Devorion.MongoDB.SchemaManager

A lightweight MongoDB library for .NET with automatic schema validation, index management, and migrations.

Installation

dotnet add package Devorion.MongoDB.SchemaManager

Quick Start

services.AddMongoSchemaSetup("mongodb://localhost:27017", "MyDatabase");

Setup with Options

services.AddMongoSchemaSetup(
    connectionString: "mongodb://localhost:27017",
    databaseName: "MyDatabase",
    registerEntities: async schema =>
    {
        schema.RegisterEntityForValidation<MyEntity>();
        schema.RegisterEntityForIndex<MyEntity, MyEntityIndexFactory>();
    },
    configureOptions: options =>
    {
        options.DefaultReadPreference = ReadPreference.SecondaryPreferred;
    }
);

Entity Definition

public class MyEntity : IMongoEntity
{
    public static string CollectionName => "my_entities";

    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; } = null!;

    [BsonElement("name")]
    public string Name { get; set; } = null!;

    [BsonElement("createdAt")]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    [NotRequired]
    [BsonElement("description")]
    public string? Description { get; set; }
}

Index Factory

public class MyEntityIndexFactory : IIndexModelsFactory<MyEntity>
{
    public static List<CreateIndexModel<MyEntity>> ConfigureIndexModels()
    {
        return new List<CreateIndexModel<MyEntity>>
        {
            new(Builders<MyEntity>.IndexKeys.Ascending(e => e.Name),
                new CreateIndexOptions { Name = "name_index" })
        };
    }
}

Usage

public class EntityService
{
    private readonly IMongoCollection<MyEntity> _collection;
    private readonly MongoDbStore _store;

    public EntityService(MongoDbStore store)
    {
        _store = store;
        _collection = store.GetCollection<MyEntity>();
    }

    // Get all
    public async Task<List<MyEntity>> GetAllAsync()
        => await _collection.All().ToListAsync();

    // Type-safe search
    public async Task<List<MyEntity>> SearchAsync(string term)
        => await _collection.Search(term, e => e.Name, e => e.Description).ToListAsync();

    // Type-safe sorting
    public async Task<List<MyEntity>> GetSortedAsync()
        => await _collection.All().ApplySorting(e => e.CreatedAt, SortDirection.Descending).ToListAsync();

    // Transaction
    public async Task AddAsync(MyEntity entity)
        => await _store.WithSessionAsync(async session =>
            await _collection.InsertOneAsync(session, entity));
}

Migrations

public class AddNameIndexMigration : IMongoMigration
{
    public string Id => "20260119_001_AddNameIndex";

    public async Task Up(IMongoDatabase db)
    {
        var collection = db.GetCollection<MyEntity>("my_entities");
        var model = new CreateIndexModel<MyEntity>(
            Builders<MyEntity>.IndexKeys.Ascending(e => e.Name),
            new CreateIndexOptions { Name = "name_index" });
        await collection.Indexes.CreateOneAsync(model);
    }
}

Migrations are auto-discovered and tracked in the _migrations collection.

Product Compatible and additional computed target framework versions.
.NET 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0-beta.2 77 1/19/2026