EasilyNET.Mongo.AspNetCore
4.25.1016.112
dotnet add package EasilyNET.Mongo.AspNetCore --version 4.25.1016.112
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 4.25.1016.112
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="EasilyNET.Mongo.AspNetCore" Version="4.25.1016.112" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasilyNET.Mongo.AspNetCore" Version="4.25.1016.112" />
<PackageReference Include="EasilyNET.Mongo.AspNetCore" />
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 EasilyNET.Mongo.AspNetCore --version 4.25.1016.112
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.Mongo.AspNetCore, 4.25.1016.112"
#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 EasilyNET.Mongo.AspNetCore@4.25.1016.112
#: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=EasilyNET.Mongo.AspNetCore&version=4.25.1016.112
#tool nuget:?package=EasilyNET.Mongo.AspNetCore&version=4.25.1016.112
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasilyNET.Mongo.AspNetCore
一个强大的 MongoDB 驱动服务包,为 ASP.NET Core 应用提供便捷的 MongoDB 数据库操作支持。
核心特性
- 字段命名转换: 数据库中字段名自动驼峰命名,ID/Id 字段自动转换为 ObjectId
- 灵活 ID 配置: 可配置部分类的 Id 字段存储为 string 类型而非 ObjectId,支持子对象和集合成员
- 时间类型本地化: 自动本地化 MongoDB 时间类型
- .NET 6+ 支持: 添加 DateOnly/TimeOnly 类型支持,可序列化为 String 或 long
- 索引管理: 支持通过特性方式自动创建和更新索引
- GridFS 文件存储: 完整的文件存储解决方案
📋 更新日志 (ChangeLogs)
- 自定义格式化: 支持自定义 TimeOnly 和 DateOnly 的格式化格式
- 支持转换为字符串格式存储
- 支持转换为 Ticks (long) 方式存储
- 可自定义实现其他类型转换,如 ulong
- 动态类型支持: 添加 object 和 dynamic 类型支持 (2.20 版本后官方已支持 JsonArray)
- JsonNode 支持: 添加 JsonNode 和 JsonObject 类型支持
添加自定义序列化支持(可选)
JsonNode 类型因为反序列化时不支持 Unicode 字符,如果需要序列化插入至其他地方(例如 Redis),在序列化时需要将 JsonSerializerOptions 的 Encoder 属性设置为 System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping.
builder.Services.AddMongoContext<DbContext>(builder.Configuration)
// 添加自定义序列化
builder.Services.RegisterSerializer(new DateOnlySerializerAsString());
builder.Services.RegisterSerializer(new TimeOnlySerializerAsString());
// 或者将他们存储为long类型的Ticks,也可以自己组合使用.
builder.Services.RegisterSerializer(new DateOnlySerializerAsTicks());
builder.Services.RegisterSerializer(new TimeOnlySerializerAsTicks());
// 添加JsonNode支持
builder.Services.RegisterSerializer(new JsonNodeSerializer());
builder.Services.RegisterSerializer(new JsonObjectSerializer());
🚀 快速开始
安装
通过 NuGet 安装 EasilyNET.Mongo.AspNetCore:
dotnet add package EasilyNET.Mongo.AspNetCore
配置连接字符串
在系统环境变量、Docker 容器或 appsettings.json
中设置 MongoDB 连接字符串:
{
"ConnectionStrings": {
"Mongo": "mongodb://localhost:27017/your-database"
}
}
或者使用环境变量:
CONNECTIONSTRINGS_MONGO=mongodb://localhost:27017/your-database
APM 监控支持
支持 APM 探针监控,基于 SkyAPM.Diagnostics.MongoDB。
📖 使用方法
方法 1: 使用默认依赖注入
var builder = WebApplication.CreateBuilder(args);
// 添加 MongoDB 数据库服务
builder.Services.AddMongoContext<DbContext>(builder.Configuration, c =>
{
// 配置数据库名称,覆盖连接字符串中的数据库名称
c.DatabaseName = "your-database";
// 配置不需要将 Id 字段存储为 ObjectId 的类型
// 使用 $unwind 操作符时,ObjectId 在转换上会有问题,所以调整为字符串
c.ObjectIdToStringTypes = new()
{
typeof(YourEntityType)
};
// 是否使用默认转换配置,包含以下内容:
// 1. 小驼峰字段名称,如: pageSize, linkPhone
// 2. 忽略代码中未定义的字段
// 3. 将 ObjectId 字段 _id 映射到实体中的 ID 或 Id 字段,反之亦然
// 4. 将枚举类型存储为字符串,如: Gender.男 存储为 "男" 而非 int 类型
c.DefaultConventionRegistry = true;
// 配置自定义 Convention
c.ConventionRegistry = new()
{
{
$"{SnowId.GenerateNewId()}",
new() { new IgnoreIfDefaultConvention(true) }
}
};
// 通过 ClientSettings 配置特殊功能
c.ClientSettings = cs =>
{
// 对接 SkyAPM 的 MongoDB 探针或其他事件订阅器
cs.ClusterConfigurator = cb => cb.Subscribe(new ActivityEventSubscriber());
};
});
// 添加 .NET 6+ 新 TimeOnly 和 DateOnly 数据类型的序列化方案
builder.Services.RegisterSerializer(new DateOnlySerializerAsString());
builder.Services.RegisterSerializer(new TimeOnlySerializerAsString());
// 注册其他序列化方案
builder.Services.RegisterSerializer(new DoubleSerializer(BsonType.Double));
var app = builder.Build();
方法 2: 使用 EasilyNET.AutoDependencyInjection
安装依赖包:
dotnet add package EasilyNET.AutoDependencyInjection
创建 EasilyNETMongoModule.cs:
public class EasilyNETMongoModule : AppModule
{
/// <summary>
/// 配置和注册服务
/// </summary>
/// <param name="context"></param>
public override void ConfigureServices(ConfigureServicesContext context)
{
var config = context.Services.GetConfiguration();
// 使用 IConfiguration 的方式注册例子,使用链接字符串,仅需将config替换成连接字符即可.
//context.Services.AddMongoContext<DbContext>(config, c =>
//{
// // 配置数据库名称,覆盖掉连接字符串中的数据库名称
// c.DatabaseName = "test23";
// // 配置不需要将Id字段存储为ObjectID的类型.使用$unwind操作符的时候,ObjectId在转换上会有一些问题,所以需要将其调整为字符串.
// c.ObjectIdToStringTypes = new()
// {
// typeof(MongoTest2)
// };
// // 是否使用默认转换配置.包含如下内容:
// // 1.小驼峰字段名称 如: pageSize ,linkPhone
// // 2.忽略代码中未定义的字段
// // 3.将ObjectID字段 _id 映射到实体中的ID或者Id字段,反之亦然.在存入数据的时候将Id或者ID映射为 _id
// // 4.将枚举类型存储为字符串, 如: Gender.男 存储到数据中为 男,而不是 int 类型
// c.DefaultConventionRegistry = true;
// c.ConventionRegistry= new()
// {
// {
// $"{SnowId.GenerateNewId()}",
// new() { new IgnoreIfDefaultConvention(true) }
// }
// };
// // 通过ClientSettings来配置一些使用特殊的东西
// c.ClientSettings = cs =>
// {
// // 对接 SkyAPM 的 MongoDB探针或者别的事件订阅器
// cs.ClusterConfigurator = cb => cb.Subscribe(new ActivityEventSubscriber());
// };
//});
//context.Services.AddMongoContext<DbContext2>(config);
//context.Services.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
// 例子二:使用MongoClientSettings配置
context.Services.AddMongoContext<DbContext>(new MongoClientSettings
{
Servers = new List<MongoServerAddress> { new("127.0.0.1", 27018) },
Credential = MongoCredential.CreateCredential("admin", "guest", "guest"),
// 对接 SkyAPM 的 MongoDB探针
ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber())
}, c =>
{
// 配置数据库名称,覆盖掉连接字符串中的数据库名称
c.DatabaseName = "test23";
// 配置不需要将Id字段存储为ObjectID的类型.使用$unwind操作符的时候,ObjectId在转换上会有一些问题.
c.ObjectIdToStringTypes = new()
{
typeof(MongoTest2)
};
// 是否使用默认转换配置.包含如下内容:
// 1.小驼峰字段名称 如: pageSize ,linkPhone
// 2.忽略代码中未定义的字段
// 3.将ObjectID字段 _id 映射到实体中的ID或者Id字段,反之亦然.在存入数据的时候将Id或者ID映射为 _id
// 4.将枚举类型存储为字符串, 如: Gender.男 存储到数据中为 男,而不是 int 类型
c.DefaultConventionRegistry = true;
c.ConventionRegistry= new()
{
{
$"{SnowId.GenerateNewId()}",
new() { new IgnoreIfDefaultConvention(true) }
}
};
});
// 注册另一个DbContext
context.Services.AddMongoContext<DbContext2>(config, c =>
{
c.DefaultConventionRegistry = true;
c.ConventionRegistry = new()
{
{
$"{SnowId.GenerateNewId()}",
new() { new IgnoreIfDefaultConvention(true) }
}
};
});
}
}
- 创建 AppWebModule.cs 并添加 EasilyNETMongoModule
/**
* 要实现自动注入,一定要在这个地方添加
*/
[DependsOn(
typeof(DependencyAppModule),
typeof(EasilyNETMongoModule)
)]
public class AppWebModule : AppModule
{
/// <summary>
/// 注册和配置服务
/// </summary>
/// <param name="context"></param>
public override void ConfigureServices(ConfigureServicesContext context)
{
base.ConfigureServices(context);
_ = context.Services.AddHttpContextAccessor();
}
/// <summary>
/// 注册中间件
/// </summary>
/// <param name="context"></param>
public override void ApplicationInitialization(ApplicationContext context)
{
base.ApplicationInitialization(context);
var app = context.GetApplicationBuilder();
_ = app.UseAuthorization();
}
}
- 最后在 Program.cs 中添加如下内容
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// 自动注入服务模块
builder.Services.AddApplication<AppWebModule>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) _ = app.UseDeveloperExceptionPage();
// 添加自动化注入的一些中间件.
app.InitializeApplication();
app.MapControllers();
app.Run();
📁 GridFS 文件存储
GridFS 是 MongoDB 的分布式文件系统,支持存储超过 16MB 的文件。
基础使用
- 注册服务:
// 需要提前注册 IMongoDatabase,或使用其他重载
builder.Services.AddMongoGridFS();
- 依赖注入使用:
public class FileService(IGridFSBucket bucket)
{
private readonly IGridFSBucket _bucket = bucket;
public async Task UploadFileAsync(Stream stream, string filename)
{
var id = await _bucket.UploadFromStreamAsync(filename, stream);
return id;
}
public async Task<Stream> DownloadFileAsync(string filename)
{
return await _bucket.OpenDownloadStreamByNameAsync(filename);
}
}
---
## 🏷️ 索引管理
EasilyNET.Mongo.AspNetCore 支持基于特性自动为实体类创建 MongoDB 索引,会根据字段命名约定(如小驼峰)自动适配索引字段名。
### 核心特性
- **单字段索引**: 使用 `[MongoIndex]` 特性声明
- **复合索引**: 使用 `[MongoCompoundIndex]` 特性声明
- **索引类型**: 支持唯一索引、文本索引、地理空间索引等
- **自动适配**: 字段名自动适配命名约定
### 使用示例
```csharp
public class User
{
[MongoIndex(EIndexType.Ascending, Unique = true)]
public string UserName { get; set; } = string.Empty;
[MongoIndex(EIndexType.Descending)]
public DateTime CreatedAt { get; set; }
}
[MongoCompoundIndex(
new[] { "UserName", "CreatedAt" },
new[] { EIndexType.Ascending, EIndexType.Descending },
Unique = true)]
public class Log
{
public string UserName { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
}
```
### 配置索引创建
```csharp
var app = builder.Build();
// 自动为所有集合创建索引,字段名自动适配命名约定
app.UseCreateMongoIndexes<DbContext>();
// 若存在多个 DbContext,需要多次应用
app.UseCreateMongoIndexes<DbContext2>();
```
### 注意事项
- 自动索引创建会比对现有索引定义
- 若定义不一致会自动删除并重建(通过名称匹配)
- 若不存在对应名称,不会删除原有索引(避免手动创建的索引失效)
---
## 📚 更多资源
- [示例项目](https://github.com/joesdu/EasilyNET/tree/main/sample)
- [API 文档](https://github.com/joesdu/EasilyNET/wiki)
- [问题反馈](https://github.com/joesdu/EasilyNET/issues)
---
_最后更新: 2025 年 9 月 3 日_
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- EasilyNET.Core (>= 4.25.1016.112)
- EasilyNET.Mongo.Core (>= 4.25.1016.112)
-
net8.0
- EasilyNET.Core (>= 4.25.1016.112)
- EasilyNET.Mongo.Core (>= 4.25.1016.112)
-
net9.0
- EasilyNET.Core (>= 4.25.1016.112)
- EasilyNET.Mongo.Core (>= 4.25.1016.112)
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 | |
---|---|---|---|
4.25.1016.112 | 114 | 10/16/2025 | |
4.25.1014.112 | 150 | 10/14/2025 | |
4.25.1013.153 | 152 | 10/13/2025 | |
4.25.921.71 | 178 | 9/20/2025 | |
4.25.919.15 | 301 | 9/18/2025 | |
4.25.918.142 | 301 | 9/18/2025 | |
4.25.918.115 | 290 | 9/18/2025 | |
4.25.918.30 | 294 | 9/17/2025 | |
4.25.917.141 | 298 | 9/17/2025 | |
4.25.917.134 | 302 | 9/17/2025 | |
4.25.911.151 | 175 | 9/11/2025 | |
4.25.905.150 | 162 | 9/5/2025 | |
4.25.820.100 | 167 | 8/20/2025 | |
4.25.814.175 | 169 | 8/14/2025 | |
4.25.731.155 | 136 | 7/31/2025 | |
4.25.724.1 | 564 | 7/23/2025 | |
4.25.720.80 | 304 | 7/20/2025 | |
4.25.716.42 | 168 | 7/15/2025 | |
4.25.617.153 | 194 | 6/17/2025 | |
4.25.616.190 | 183 | 6/16/2025 | |
4.25.616.182 | 175 | 6/16/2025 | |
4.25.613.111 | 312 | 6/13/2025 | |
4.25.609.190 | 858 | 6/9/2025 | |
3.24.929.131 | 308 | 9/29/2024 | |
3.24.929.122 | 298 | 9/29/2024 | |
3.24.926.184 | 306 | 9/26/2024 | |
3.24.926.182 | 300 | 9/26/2024 | |
3.24.926.175 | 297 | 9/26/2024 | |
3.24.924.160 | 305 | 9/24/2024 | |
3.24.924.133 | 308 | 9/24/2024 | |
3.24.924.124 | 294 | 9/24/2024 | |
3.24.924.10 | 300 | 9/23/2024 | |
3.24.924.1 | 298 | 9/23/2024 | |
3.24.923.234 | 294 | 9/23/2024 | |
3.24.923.232 | 300 | 9/23/2024 | |
3.24.923.155 | 301 | 9/23/2024 | |
3.24.919.92 | 299 | 9/19/2024 | |
3.24.914.125 | 464 | 9/14/2024 | |
3.24.914.115 | 324 | 9/14/2024 | |
3.24.914.111 | 298 | 9/14/2024 | |
3.24.911.95 | 295 | 9/11/2024 | |
3.24.908.215 | 290 | 9/8/2024 | |
3.24.904.200 | 446 | 9/4/2024 | |
3.24.828.163 | 299 | 8/28/2024 | |
3.24.820.173 | 303 | 8/20/2024 | |
3.24.814.92 | 312 | 8/14/2024 | |
3.24.812.115 | 304 | 8/12/2024 | |
3.24.802.100 | 279 | 8/2/2024 | |
3.24.801.162 | 294 | 8/1/2024 | |
3.24.801.160 | 290 | 8/1/2024 | |
3.24.801.155 | 291 | 8/1/2024 | |
3.24.730.164 | 281 | 7/30/2024 | |
3.24.730.91 | 282 | 7/30/2024 | |
3.24.724.91 | 287 | 7/24/2024 | |
3.24.718.105 | 298 | 7/18/2024 | |
3.24.716.95 | 293 | 7/16/2024 | |
3.24.712.94 | 292 | 7/12/2024 | |
3.24.710.14 | 293 | 7/9/2024 | |
3.24.709.105 | 293 | 7/9/2024 | |
3.24.704.94 | 302 | 7/4/2024 | |
3.24.701.90 | 299 | 7/1/2024 | |
3.24.628.114 | 305 | 6/28/2024 | |
3.24.627.145 | 293 | 6/27/2024 | |
3.24.620.160 | 304 | 6/20/2024 | |
3.24.613.115 | 301 | 6/13/2024 | |
3.24.612.95 | 292 | 6/12/2024 | |
3.24.528.90 | 299 | 5/28/2024 | |
3.24.522.84 | 308 | 5/22/2024 | |
3.24.512.213 | 300 | 5/12/2024 | |
3.24.508.112 | 320 | 5/8/2024 | |
2.2024.428.71 | 295 | 4/28/2024 | |
2.2024.427.1128 | 296 | 4/27/2024 | |
2.2.72 | 377 | 4/14/2024 | |
2.2.71 | 308 | 4/12/2024 | |
2.2.8 | 297 | 4/26/2024 | |
2.2.6 | 305 | 4/10/2024 | |
2.2.5 | 312 | 3/26/2024 | |
2.2.4 | 315 | 3/25/2024 | |
2.2.3 | 312 | 3/24/2024 | |
2.2.2 | 313 | 3/21/2024 | |
2.2.1 | 314 | 3/20/2024 | |
2.2.0 | 314 | 3/13/2024 | |
2.1.9 | 317 | 2/21/2024 | |
2.1.8 | 305 | 2/18/2024 | |
2.1.7 | 312 | 2/16/2024 | |
2.1.6 | 331 | 2/14/2024 | |
2.1.5 | 308 | 2/14/2024 | |
1.9.1 | 341 | 11/1/2023 | |
1.9.0 | 338 | 10/19/2023 | |
1.9.0-preview2 | 528 | 10/12/2023 | |
1.9.0-preview1 | 300 | 10/12/2023 | |
1.8.9 | 376 | 10/11/2023 | |
1.8.8 | 350 | 10/11/2023 | |
1.8.7-rc2 | 319 | 9/21/2023 | |
1.8.7-rc1 | 309 | 9/12/2023 | |
1.8.6 | 363 | 8/31/2023 | |
1.8.5 | 1,059 | 8/25/2023 | |
1.8.4 | 362 | 8/24/2023 | |
1.8.3 | 370 | 8/23/2023 | |
1.8.2 | 441 | 8/22/2023 | |
1.8.1 | 384 | 8/18/2023 | |
1.8.0 | 365 | 8/15/2023 | |
1.7.9 | 404 | 8/11/2023 | |
1.7.8 | 347 | 8/11/2023 | |
1.7.7 | 392 | 8/10/2023 | |
1.7.6 | 384 | 8/9/2023 | |
1.7.5 | 440 | 8/9/2023 | |
1.7.4 | 472 | 8/3/2023 | |
1.7.3 | 387 | 8/1/2023 | |
1.7.2 | 375 | 7/31/2023 | |
1.7.1 | 375 | 7/27/2023 | |
1.7.0 | 387 | 7/25/2023 | |
1.6.9 | 377 | 7/25/2023 | |
1.6.8 | 372 | 7/24/2023 | |
1.6.7 | 399 | 7/20/2023 | |
1.6.6 | 401 | 7/19/2023 | |
1.6.5 | 360 | 7/19/2023 | |
1.6.4 | 378 | 7/17/2023 | |
1.6.3 | 353 | 7/17/2023 | |
1.6.2 | 416 | 7/12/2023 | |
1.6.1 | 412 | 6/30/2023 | |
1.6.0 | 349 | 6/30/2023 |