{"id":20274882,"url":"https://github.com/mgernand/mongodb.dbcontext","last_synced_at":"2025-12-24T02:02:30.805Z","repository":{"id":152507255,"uuid":"625563414","full_name":"mgernand/MongoDB.DbContext","owner":"mgernand","description":"A database context implementation for MongoDB.","archived":false,"fork":false,"pushed_at":"2025-03-17T08:55:58.000Z","size":65,"stargazers_count":3,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-17T23:18:02.895Z","etag":null,"topics":["database","database-migration","database-schema","dbcontext","dotnet","dotnet-core","dotnetcore","mongo","mongodb","mongodb-database","mongodb-driver"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mgernand.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-09T14:00:31.000Z","updated_at":"2025-07-28T12:34:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"cb8184f9-efa6-44e0-a810-b556ed1265a7","html_url":"https://github.com/mgernand/MongoDB.DbContext","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"purl":"pkg:github/mgernand/MongoDB.DbContext","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FMongoDB.DbContext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FMongoDB.DbContext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FMongoDB.DbContext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FMongoDB.DbContext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mgernand","download_url":"https://codeload.github.com/mgernand/MongoDB.DbContext/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mgernand%2FMongoDB.DbContext/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27992996,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","database-migration","database-schema","dbcontext","dotnet","dotnet-core","dotnetcore","mongo","mongodb","mongodb-database","mongodb-driver"],"created_at":"2024-11-14T13:06:39.463Z","updated_at":"2025-12-24T02:02:30.798Z","avatar_url":"https://github.com/mgernand.png","language":"C#","readme":"# MongoDB.DbContext\n\nA DB context implementation for MongoDB.\n\nThe DB context implementation is very heavily inspired by the EntityFramework Core ```DbContext```\ninfrastructure. The options configuration works the same. The database options can be set when\nconfiguring the context, or agail later (and maybe altered) when new instances of the context\nare created.\n\nThe context services are registered as scoped automatically. Every context will get the MongoDB\ndriver services (```IMongoDatabase```, ```IMongoClient```) from an interner service provider which\nwill have those services registered as singleton instances. This cache of internal service providers\nuses the conenction string, str database name and the context options type as key, so changing the\ndatabase name for a context at runtime (by overriding ```OnConfiguring```) will result in a new internal\nservice provider with the corresponding MongoDB singleton services.\n\n## Usage\n\nTo add a ```MongoDbContext``` one uses the ```AddMongoDbContext``` extension method. There\nare several ways to configure one or multiple contexts.\n\n### Configuration without custom context class\n\nTo get started quickly, one can simply use the ```MongoDbContext``` class and configure the\ndatabase by passing an options builder action to ```AddMongoDbContext```. This way you have\nno possibility to further configure the context at runtime.\n\n```C#\nservices.AddMongoDbContext\u003cMongoDbContext\u003e(builder =\u003e\n{\n\tbuilder.UseDatabase(\"mongodb://localhost:27017\", \"test\");\n});\n```\n\n### Configuration with custom context class\n\nIf you need more control ove the context configuration or need to add multiple different\ncontexts, you need to dervice you context class from ```MongoDbContext```\n\n```C#\nservices.AddMongoDbContext\u003cSampleContext\u003e(builder =\u003e\n{\n\tbuilder.UseDatabase(\"mongodb://localhost:27017\", \"test\");\n});\n```\n\n```C#\npublic sealed class SampleContext : MongoDbContext\n{\n\tpublic SampleContext(MongoDbContextOptions options)\n\t\t: base(options)\n\t{\n\t}\n\n\t/// \u003cinheritdoc /\u003e\n\tprotected override void OnConfiguring(MongoDbContextOptionsBuilder builder)\n\t{\n\t\tbuilder.UseDatabase(\"mongodb://localhost:27017\", \"other\");\n\t}\n}\n```\n\nThe ```OnConfiguring``` method is called everytime a new instance of the context is created,\nso it is possible to change the connection settings for the context based on runtime information.\n\n### Configuring multiple different context classes\n\nIf you need to add several contexts you need to inject the generic version of the ```MongoDbContextOptions```\nto allow the correct options be created. There will be an exception thrown if you don't do it.\n\n```C#\nservices.AddMongoDbContext\u003cSampleContextOne\u003e(builder =\u003e\n{\n\tbuilder.UseDatabase(\"mongodb://localhost:27017\", \"one\");\n});\n\nservices.AddMongoDbContext\u003cSampleContextTwo\u003e(builder =\u003e\n{\n\tbuilder.UseDatabase(\"mongodb://localhost:27017\", \"two\");\n});\n```\n\n```C#\npublic sealed class SampleContextOne : MongoDbContext\n{\n\tpublic SampleContext(MongoDbContextOptions\u003cSampleContextOne\u003e options)\n\t\t: base(options)\n\t{\n\t}\n}\n\npublic sealed class SampleContextTwo : MongoDbContext\n{\n\tpublic SampleContext(MongoDbContextOptions\u003cSampleContextTwo\u003e options)\n\t\t: base(options)\n\t{\n\t}\n}\n```\n\n### Configuring telemetry information\n\nTo expose telemetry information via ```System.Diagnostics``` just enable it in the ```MongoDbContextOptions```.\nIf you like to integrate this telemetry into OpenTelemetry just add a source with the name\n```MongoDB.Driver.Core.Extensions.DiagnosticSources```.\n\n```C#\nservices.AddMongoDbContext\u003cSampleContext\u003e(builder =\u003e\n{\n\tbuilder\n\t\t.UseDatabase(\"mongodb://localhost:27017\", \"test\")\n\t\t.EnableTelemetry();\n});\n```\n\n\n## References\n\n[EntityFramework Core](https://github.com/dotnet/efcore)\n\n[MongoDB.Driver.Core.Extensions.DiagnosticSources](https://github.com/jbogard/MongoDB.Driver.Core.Extensions.DiagnosticSources)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgernand%2Fmongodb.dbcontext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmgernand%2Fmongodb.dbcontext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmgernand%2Fmongodb.dbcontext/lists"}