CSharp.Mongo.Migration
3.8.1
dotnet add package CSharp.Mongo.Migration --version 3.8.1
NuGet\Install-Package CSharp.Mongo.Migration -Version 3.8.1
<PackageReference Include="CSharp.Mongo.Migration" Version="3.8.1" />
<PackageVersion Include="CSharp.Mongo.Migration" Version="3.8.1" />
<PackageReference Include="CSharp.Mongo.Migration" />
paket add CSharp.Mongo.Migration --version 3.8.1
#r "nuget: CSharp.Mongo.Migration, 3.8.1"
#:package CSharp.Mongo.Migration@3.8.1
#addin nuget:?package=CSharp.Mongo.Migration&version=3.8.1
#tool nuget:?package=CSharp.Mongo.Migration&version=3.8.1
CSharp Mongo Migrations
Simple script based MongoDB database migrations written and run in .NET.
CSharp.Mongo.Migration uses the C# MongoDB Driver to update documents in script based single use migrations (rather than on-the-fly).
See the Wiki for more information.
Status
Usage
Installation
Nuget: https://www.nuget.org/packages/CSharp.Mongo.Migration
Install using the Visual Studio package manager or the dotnet CLI:
dotnet add package CSharp.Mongo.Migration
Running Migrations
- Create an instance of the
MigrationRunnerclass
MigrationRunner runner = new("yourDatabaseConnectionString");
- Register an
IMigrationLocatorand run the migrations
await runner
.RegisterLocator(new AssemblyMigrationLocator("Assembly.With.Your.Migrations.dll"))
.RunAsync();
Writing Migrations
A migration script should implement the IMigration or IAsyncMigration interface. The following properties / methods will need to be defined:
string Namestring Versionvoid Up(IMongoDatabase)/Task UpAsync(IMongoDatabase database)void Down(IMongoDatabase)/Task DownAsync(IMongoDatabase database)
An optional bool Skip() method can be defined that can be used to conditionally skip a migration.
Up / UpAsync will be run when the MigrationRunner.RunAsync method is called.
Down / DownAsync will be called when the MigrationRunner.RevertAsync(string) method is called, if you don't expect to revert a migration, simply have this method throw an exception.
Explicit Order
If a migration requires or depends on another migration script, the IOrderedMigration interface can be used and it's DependsOn property implemented to provide version dependencies between migrations. Migrations need to implement one of the IMigration or IAsyncMigration interfaces along with the IOrderedMigration interface to be loaded and run.
Ignoring Migrations
If a migration is no longer required, you can remove the class from your application, or use the [IgnoreMigration] attribute on that class.
For example:
[IgnoreMigration]
public class MyMigration1 : IMigration { }
Restoring Migrations
To revert or restore a migration run the RevertAsync("version") function on an instance of the MigrationRunner class
MigrationRunner runner = new("youDatabaseConnectionString");
await runner
.RegisterLocator(new AssemblyMigrationLocator("Assembly.With.Migration.Version.dll"))
.RevertAsync("version");
Migration Documents
When a migration has been applied a new document will be created in the target database in a collection named _migrations by default. These documents are used to track which migrations have been applied and when they were applied.
Note: to override the default collection name, use a constructor that includes the migrationCollectionName parameter, or set the public MigrationRunner.MigrationCollectionName property.
Logging
This library provides support for logging using Microsoft.Extensions.Logging.
The MigrationRunner class writes information level logs that help to understand which migrations have been located and when they have successfully completed. In order to configure logging use the RegisterLogger function.
Why Create Another Library?
There are a number of other libraries for MongoDB designed to be used in C#. This library was written out of necessity to be used in a large development team with fairly sizeable database instances. There are some use cases that this library caters to that others do not:
- Allow migrations to run out of order
- Provide a version identifier that is not a number or using SemVer
- Encourage
asyncmigration scripts
Using other libraries we found that migration versions could clash when using number or SemVer based identifiers, our team uses GitFlow to achieve the development throughput and organisation that we require and often larger feature branches would clash when selecting version numbers.
Dependencies
This library uses the following packages:
Development Dependencies
We use the following libraries for testing / development purposes:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. 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 was computed. 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. |
| .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
- MongoDB.Driver (>= 3.8.1)
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 | |
|---|---|---|---|
| 3.8.1 | 101 | 5/18/2026 | |
| 3.8.0 | 131 | 4/27/2026 | |
| 3.7.1 | 3,149 | 3/23/2026 | |
| 3.7.0 | 127 | 3/9/2026 | |
| 3.6.0 | 7,583 | 1/19/2026 | |
| 3.5.2 | 795 | 12/1/2025 | |
| 3.5.1 | 353 | 11/25/2025 | |
| 3.5.0 | 4,649 | 9/15/2025 | |
| 3.4.3 | 790 | 8/25/2025 | |
| 3.4.2 | 635 | 7/28/2025 | |
| 3.4.0 | 11,897 | 5/18/2025 | |
| 3.0.0 | 1,540 | 11/7/2024 | |
| 2.0.0 | 1,320 | 11/7/2024 | |
| 1.4.0 | 859 | 8/5/2024 | |
| 1.3.0 | 489 | 7/20/2024 | |
| 1.2.0 | 504 | 5/24/2024 | |
| 1.1.1 | 932 | 5/20/2024 | |
| 1.1.0 | 567 | 4/13/2024 | |
| 1.0.0 | 573 | 4/6/2024 | |
| 0.0.4-alpha | 213 | 4/5/2024 |