EasilyNET.Mongo.AspNetCore
3.24.1216.116
dotnet add package EasilyNET.Mongo.AspNetCore --version 3.24.1216.116
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 3.24.1216.116
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="3.24.1216.116" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.Mongo.AspNetCore --version 3.24.1216.116
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.Mongo.AspNetCore, 3.24.1216.116"
#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.
// Install EasilyNET.Mongo.AspNetCore as a Cake Addin #addin nuget:?package=EasilyNET.Mongo.AspNetCore&version=3.24.1216.116 // Install EasilyNET.Mongo.AspNetCore as a Cake Tool #tool nuget:?package=EasilyNET.Mongo.AspNetCore&version=3.24.1216.116
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
EasilyNET.Mongo.AspNetCore
- 一个 MongoDB 驱动的服务包,方便使用 MongoDB 数据库.
- 数据库中字段名驼峰命名,ID,Id 自动转化成 ObjectId.
- 可配置部分类的 Id 字段不存为 ObjectId,而存为 string 类型.支持子对象以及集合成员的 Id 字段转化.
- 自动本地化 MongoDB 时间类型
- 添加.Net6 Date/Time Only 类型支持(序列化到 String 或 long)
ChangeLogs
- 支持自定义 TimeOnly 和 DateOnly 的格式化格式.
- 支持转换成字符串格式
- 转换成 Ticks 的方式存储
- 若想转化成其他类型也可自行实现,如:转化成 ulong 类型
- 添加动态类型支持[object 和 dynamic], 2.20 版后官方又加上了. JsonArray.
- 添加JsonNode类型支持.
添加自定义序列化支持(可选)
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());
使用
- Nuget 安装 EasilyNET.Mongo.AspNetCore
- 在系统环境变量或者 Docker 容器中设置环境变量名称为: CONNECTIONSTRINGS_MONGO = mongodb 链接字符串 或者在 appsettings.json 中添加,
- 现在你也可以参考 example.api 项目查看直接传入相关数据.
- 添加 APM 探针支持,根据 SkyApm.Diagnostics.MongoDB
{
"ConnectionStrings": {
"Mongo": "mongodb链接字符串"
},
// 或者使用
"CONNECTIONSTRINGS_MONGO": "mongodb链接字符串"
}
方法 1. 使用默认依赖注入方式
var builder = WebApplication.CreateBuilder(args);
// 添加Mongodb数据库服务
builder.Services.AddMongoContext<DbContext>(builder.Configuration, 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;
// 配置自定义Convention
c.ConventionRegistry= new()
{
{
$"{SnowId.GenerateNewId()}",
new() { new IgnoreIfDefaultConvention(true) }
}
};
// 通过ClientSettings来配置一些使用特殊的东西
c.ClientSettings = cs =>
{
// 对接 SkyAPM 的 MongoDB探针或者别的事件订阅器
cs.ClusterConfigurator = cb => cb.Subscribe(new ActivityEventSubscriber());
};
});
// 添加.NET6+新的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
- 项目添加 EasilyNET.AutoDependencyInjection Nuget 包
- 创建 EasilyNETMongoModule.cs 并继承 AppModule 类
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
- 注册服务
// 需要提前注册 IMongoDatabase, 或者使用其他重载来注册服务.
builder.Services.AddMongoGridFS();
- 使用依赖注入获取 GridFSBucket 操作 GridFS
public class YourClass(IGridFSBucket bucket)
{
private readonly IGridFSBucket _bucket = bucket;
public void DoSomething()
{
_bucket.XXXXXX();
}
}
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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- EasilyNET.Core (>= 3.24.1216.116)
- EasilyNET.Mongo.Core (>= 3.24.1216.116)
-
net9.0
- EasilyNET.Core (>= 3.24.1216.116)
- EasilyNET.Mongo.Core (>= 3.24.1216.116)
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.24.1216.116 | 82 | 12/16/2024 |
3.24.1206.100 | 83 | 12/6/2024 |
3.24.1205.171 | 93 | 12/5/2024 |
3.24.1202.150 | 72 | 12/2/2024 |
3.24.1126.231 | 76 | 11/26/2024 |
3.24.1126.172 | 83 | 11/26/2024 |
3.24.1126.114 | 80 | 11/26/2024 |
3.24.1126.104 | 81 | 11/26/2024 |
3.24.1125.181 | 78 | 11/25/2024 |
3.24.1125.104 | 84 | 11/25/2024 |
3.24.1121.183 | 76 | 11/21/2024 |
3.24.1120.183 | 76 | 11/20/2024 |
3.24.1119.31 | 80 | 11/18/2024 |
3.24.1115.143 | 76 | 11/15/2024 |
3.24.1113.100 | 74 | 11/13/2024 |
3.24.1112.125 | 161 | 11/12/2024 |
3.24.1107.140 | 81 | 11/7/2024 |
3.24.1107.54 | 79 | 11/7/2024 |
3.24.1107.34 | 71 | 11/7/2024 |
3.24.1105.111 | 86 | 11/5/2024 |
3.24.1103.31 | 78 | 11/2/2024 |
3.24.1103 | 83 | 11/2/2024 |
3.24.1031.135 | 80 | 10/31/2024 |
3.24.1031.112 | 71 | 10/31/2024 |
3.24.1031.104 | 78 | 10/31/2024 |
3.24.1029.142 | 80 | 10/29/2024 |
3.24.1025.30 | 185 | 10/24/2024 |
3.24.1022.142 | 68 | 10/22/2024 |
3.24.1018.204 | 136 | 10/18/2024 |
3.24.1018.175 | 127 | 10/18/2024 |
3.24.1018.166 | 130 | 10/18/2024 |
3.24.1018.93 | 131 | 10/18/2024 |
3.24.1017.42 | 98 | 10/16/2024 |
3.24.1016.161 | 95 | 10/16/2024 |
3.24.1015.231 | 81 | 10/15/2024 |
3.24.1015.14 | 81 | 10/14/2024 |
3.24.1012.114 | 86 | 10/12/2024 |
3.24.1009.115 | 92 | 10/9/2024 |
3.24.1008.160 | 77 | 10/8/2024 |
3.24.1008.133 | 85 | 10/8/2024 |
3.24.1007.185 | 87 | 10/7/2024 |
3.24.1003.33 | 96 | 10/2/2024 |
3.24.1002.162 | 95 | 10/2/2024 |
3.24.929.143 | 229 | 9/29/2024 |
3.24.929.141 | 89 | 9/29/2024 |
3.24.929.131 | 66 | 9/29/2024 |
3.24.929.122 | 57 | 9/29/2024 |
3.24.926.184 | 62 | 9/26/2024 |
3.24.926.182 | 59 | 9/26/2024 |
3.24.926.175 | 60 | 9/26/2024 |
3.24.924.160 | 62 | 9/24/2024 |
3.24.924.133 | 63 | 9/24/2024 |
3.24.924.124 | 54 | 9/24/2024 |
3.24.924.10 | 58 | 9/23/2024 |
3.24.924.1 | 59 | 9/23/2024 |
3.24.923.234 | 51 | 9/23/2024 |
3.24.923.232 | 56 | 9/23/2024 |
3.24.923.155 | 56 | 9/23/2024 |
3.24.919.92 | 64 | 9/19/2024 |
3.24.914.125 | 232 | 9/14/2024 |
3.24.914.115 | 82 | 9/14/2024 |
3.24.914.111 | 62 | 9/14/2024 |
3.24.911.95 | 64 | 9/11/2024 |
3.24.908.215 | 58 | 9/8/2024 |
3.24.904.200 | 211 | 9/4/2024 |
3.24.828.163 | 70 | 8/28/2024 |
3.24.820.173 | 69 | 8/20/2024 |
3.24.814.92 | 76 | 8/14/2024 |
3.24.812.115 | 70 | 8/12/2024 |
3.24.802.100 | 47 | 8/2/2024 |
3.24.801.162 | 57 | 8/1/2024 |
3.24.801.160 | 54 | 8/1/2024 |
3.24.801.155 | 55 | 8/1/2024 |
3.24.730.164 | 47 | 7/30/2024 |
3.24.730.91 | 47 | 7/30/2024 |
3.24.724.91 | 48 | 7/24/2024 |
3.24.718.105 | 64 | 7/18/2024 |
3.24.716.95 | 59 | 7/16/2024 |
3.24.712.94 | 57 | 7/12/2024 |
3.24.710.14 | 59 | 7/9/2024 |
3.24.709.105 | 60 | 7/9/2024 |
3.24.704.94 | 62 | 7/4/2024 |
3.24.701.90 | 62 | 7/1/2024 |
3.24.628.114 | 69 | 6/28/2024 |
3.24.627.145 | 56 | 6/27/2024 |
3.24.620.160 | 64 | 6/20/2024 |
3.24.613.115 | 56 | 6/13/2024 |
3.24.612.95 | 59 | 6/12/2024 |
3.24.528.90 | 63 | 5/28/2024 |
3.24.522.84 | 71 | 5/22/2024 |
3.24.512.213 | 66 | 5/12/2024 |
3.24.508.112 | 82 | 5/8/2024 |
2.2024.428.71 | 63 | 4/28/2024 |
2.2024.427.1128 | 66 | 4/27/2024 |
2.2.72 | 137 | 4/14/2024 |
2.2.71 | 76 | 4/12/2024 |
2.2.8 | 61 | 4/26/2024 |
2.2.6 | 68 | 4/10/2024 |
2.2.5 | 82 | 3/26/2024 |
2.2.4 | 82 | 3/25/2024 |
2.2.3 | 77 | 3/24/2024 |
2.2.2 | 77 | 3/21/2024 |
2.2.1 | 78 | 3/20/2024 |
2.2.0 | 79 | 3/13/2024 |
2.1.9 | 81 | 2/21/2024 |
2.1.8 | 69 | 2/18/2024 |
2.1.7 | 75 | 2/16/2024 |
2.1.6 | 93 | 2/14/2024 |
2.1.5 | 72 | 2/14/2024 |
2.1.4 | 156 | 2/9/2024 |
2.1.3 | 122 | 2/8/2024 |
2.1.2 | 144 | 2/5/2024 |
2.1.1.2 | 198 | 12/26/2023 |
2.1.1.1 | 124 | 12/26/2023 |
2.1.1 | 130 | 12/25/2023 |
2.1.0 | 165 | 12/17/2023 |
2.0.11 | 199 | 12/6/2023 |
2.0.1 | 203 | 11/15/2023 |
2.0.0 | 147 | 11/14/2023 |
1.9.1 | 119 | 11/1/2023 |
1.9.0 | 110 | 10/19/2023 |
1.9.0-preview2 | 284 | 10/12/2023 |
1.9.0-preview1 | 77 | 10/12/2023 |
1.8.9 | 149 | 10/11/2023 |
1.8.8 | 128 | 10/11/2023 |
1.8.7-rc2 | 99 | 9/21/2023 |
1.8.7-rc1 | 85 | 9/12/2023 |
1.8.6 | 128 | 8/31/2023 |
1.8.5 | 788 | 8/25/2023 |
1.8.4 | 123 | 8/24/2023 |
1.8.3 | 126 | 8/23/2023 |
1.8.2 | 205 | 8/22/2023 |
1.8.1 | 142 | 8/18/2023 |
1.8.0 | 122 | 8/15/2023 |
1.7.9 | 160 | 8/11/2023 |
1.7.8 | 106 | 8/11/2023 |
1.7.7 | 151 | 8/10/2023 |
1.7.6 | 135 | 8/9/2023 |
1.7.5 | 191 | 8/9/2023 |
1.7.4 | 227 | 8/3/2023 |
1.7.3 | 143 | 8/1/2023 |
1.7.2 | 130 | 7/31/2023 |
1.7.1 | 133 | 7/27/2023 |
1.7.0 | 142 | 7/25/2023 |
1.6.9 | 134 | 7/25/2023 |
1.6.8 | 128 | 7/24/2023 |
1.6.7 | 148 | 7/20/2023 |
1.6.6 | 154 | 7/19/2023 |
1.6.5 | 108 | 7/19/2023 |
1.6.4 | 128 | 7/17/2023 |
1.6.3 | 113 | 7/17/2023 |
1.6.2 | 166 | 7/12/2023 |
1.6.1 | 162 | 6/30/2023 |
1.6.0 | 100 | 6/30/2023 |