EasilyNET.Mongo.AspNetCore
2.2.6
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package EasilyNET.Mongo.AspNetCore --version 2.2.6
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 2.2.6
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="2.2.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.Mongo.AspNetCore --version 2.2.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EasilyNET.Mongo.AspNetCore, 2.2.6"
#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=2.2.6 // Install EasilyNET.Mongo.AspNetCore as a Cake Tool #tool nuget:?package=EasilyNET.Mongo.AspNetCore&version=2.2.6
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 类型.
- 自动本地化 MongoDB 时间类型
- 添加.Net6 Date/Time Only 类型支持
- 添加 SkyWalking-APM 探针支持,未依赖 Agent,所以需要手动传入参数.
使用
- Nuget 安装 EasilyNET.Mongo.AspNetCore
- 推荐同时安装 EasilyNET.MongoSerializer.AspNetCore 包,添加了对 .Net6+ 的 Date/Time Only 类型
- 在系统环境变量或者 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 =>
{
// 新版的MongoDB驱动默认为V3,老项目会出现一些问题,可设置V2来兼容老项目
cs.LinqProvider = LinqProvider.V2;
// 对接 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 =>
// {
// // 新版的MongoDB驱动默认为V3,老项目会出现一些问题,可设置V2来兼容老项目
// cs.LinqProvider = LinqProvider.V2;
// // 对接 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"),
// 新版驱动使用V3版本,有可能会出现一些Linq表达式客户端函数无法执行,需要调整代码,但是工作量太大了,所以可以先使用V2兼容.
LinqProvider = LinqProvider.V3,
// 对接 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();
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.Mongo.Core (>= 2.2.6)
- Microsoft.Extensions.Configuration.Abstractions (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
-
net9.0
- EasilyNET.Mongo.Core (>= 2.2.6)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-preview.2.24128.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0-preview.2.24128.5)
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.1126.231 | 61 | 11/26/2024 |
3.24.1126.172 | 69 | 11/26/2024 |
3.24.1126.114 | 68 | 11/26/2024 |
3.24.1126.104 | 58 | 11/26/2024 |
3.24.1125.181 | 58 | 11/25/2024 |
3.24.1125.104 | 69 | 11/25/2024 |
3.24.1121.183 | 65 | 11/21/2024 |
3.24.1120.183 | 66 | 11/20/2024 |
3.24.1119.31 | 70 | 11/18/2024 |
3.24.1115.143 | 65 | 11/15/2024 |
3.24.1113.100 | 62 | 11/13/2024 |
3.24.1112.125 | 136 | 11/12/2024 |
3.24.1107.140 | 73 | 11/7/2024 |
3.24.1107.54 | 69 | 11/7/2024 |
3.24.1107.34 | 63 | 11/7/2024 |
3.24.1105.111 | 77 | 11/5/2024 |
3.24.1103.31 | 69 | 11/2/2024 |
3.24.1103 | 74 | 11/2/2024 |
3.24.1031.135 | 71 | 10/31/2024 |
3.24.1031.112 | 64 | 10/31/2024 |
3.24.1031.104 | 71 | 10/31/2024 |
3.24.1029.142 | 67 | 10/29/2024 |
3.24.1025.30 | 174 | 10/24/2024 |
3.24.1022.142 | 59 | 10/22/2024 |
3.24.1018.204 | 129 | 10/18/2024 |
3.24.1018.175 | 120 | 10/18/2024 |
3.24.1018.166 | 123 | 10/18/2024 |
3.24.1018.93 | 124 | 10/18/2024 |
3.24.1017.42 | 89 | 10/16/2024 |
3.24.1016.161 | 88 | 10/16/2024 |
3.24.1015.231 | 72 | 10/15/2024 |
3.24.1015.14 | 74 | 10/14/2024 |
3.24.1012.114 | 77 | 10/12/2024 |
3.24.1009.115 | 81 | 10/9/2024 |
3.24.1008.160 | 70 | 10/8/2024 |
3.24.1008.133 | 74 | 10/8/2024 |
3.24.1007.185 | 78 | 10/7/2024 |
3.24.1003.33 | 85 | 10/2/2024 |
3.24.1002.162 | 86 | 10/2/2024 |
3.24.929.143 | 222 | 9/29/2024 |
3.24.929.141 | 82 | 9/29/2024 |
3.24.929.131 | 59 | 9/29/2024 |
3.24.929.122 | 50 | 9/29/2024 |
3.24.926.184 | 52 | 9/26/2024 |
3.24.926.182 | 52 | 9/26/2024 |
3.24.926.175 | 50 | 9/26/2024 |
3.24.924.160 | 55 | 9/24/2024 |
3.24.924.133 | 56 | 9/24/2024 |
3.24.924.124 | 45 | 9/24/2024 |
3.24.924.10 | 51 | 9/23/2024 |
3.24.924.1 | 50 | 9/23/2024 |
3.24.923.234 | 44 | 9/23/2024 |
3.24.923.232 | 49 | 9/23/2024 |
3.24.923.155 | 49 | 9/23/2024 |
3.24.919.92 | 51 | 9/19/2024 |
3.24.914.125 | 223 | 9/14/2024 |
3.24.914.115 | 73 | 9/14/2024 |
3.24.914.111 | 55 | 9/14/2024 |
3.24.911.95 | 57 | 9/11/2024 |
3.24.908.215 | 51 | 9/8/2024 |
3.24.904.200 | 204 | 9/4/2024 |
3.24.828.163 | 61 | 8/28/2024 |
3.24.820.173 | 62 | 8/20/2024 |
3.24.814.92 | 69 | 8/14/2024 |
3.24.812.115 | 61 | 8/12/2024 |
3.24.802.100 | 39 | 8/2/2024 |
3.24.801.162 | 50 | 8/1/2024 |
3.24.801.160 | 47 | 8/1/2024 |
3.24.801.155 | 48 | 8/1/2024 |
3.24.730.164 | 38 | 7/30/2024 |
3.24.730.91 | 38 | 7/30/2024 |
3.24.724.91 | 41 | 7/24/2024 |
3.24.718.105 | 56 | 7/18/2024 |
3.24.716.95 | 52 | 7/16/2024 |
3.24.712.94 | 50 | 7/12/2024 |
3.24.710.14 | 52 | 7/9/2024 |
3.24.709.105 | 49 | 7/9/2024 |
3.24.704.94 | 55 | 7/4/2024 |
3.24.701.90 | 55 | 7/1/2024 |
3.24.628.114 | 62 | 6/28/2024 |
3.24.627.145 | 48 | 6/27/2024 |
3.24.620.160 | 55 | 6/20/2024 |
3.24.613.115 | 49 | 6/13/2024 |
3.24.612.95 | 52 | 6/12/2024 |
3.24.528.90 | 56 | 5/28/2024 |
3.24.522.84 | 62 | 5/22/2024 |
3.24.512.213 | 60 | 5/12/2024 |
3.24.508.112 | 73 | 5/8/2024 |
2.2024.428.71 | 58 | 4/28/2024 |
2.2024.427.1128 | 59 | 4/27/2024 |
2.2.72 | 132 | 4/14/2024 |
2.2.71 | 69 | 4/12/2024 |
2.2.8 | 56 | 4/26/2024 |
2.2.6 | 63 | 4/10/2024 |
2.2.5 | 77 | 3/26/2024 |
2.2.4 | 77 | 3/25/2024 |
2.2.3 | 72 | 3/24/2024 |
2.2.2 | 72 | 3/21/2024 |
2.2.1 | 71 | 3/20/2024 |
2.2.0 | 74 | 3/13/2024 |
2.1.9 | 76 | 2/21/2024 |
2.1.8 | 64 | 2/18/2024 |
2.1.7 | 70 | 2/16/2024 |
2.1.6 | 88 | 2/14/2024 |
2.1.5 | 67 | 2/14/2024 |
2.1.4 | 147 | 2/9/2024 |
2.1.3 | 113 | 2/8/2024 |
2.1.2 | 139 | 2/5/2024 |
2.1.1.2 | 193 | 12/26/2023 |
2.1.1.1 | 119 | 12/26/2023 |
2.1.1 | 125 | 12/25/2023 |
2.1.0 | 160 | 12/17/2023 |
2.0.11 | 194 | 12/6/2023 |
2.0.1 | 196 | 11/15/2023 |
2.0.0 | 142 | 11/14/2023 |
1.9.1 | 114 | 11/1/2023 |
1.9.0 | 103 | 10/19/2023 |
1.9.0-preview2 | 278 | 10/12/2023 |
1.9.0-preview1 | 74 | 10/12/2023 |
1.8.9 | 144 | 10/11/2023 |
1.8.8 | 123 | 10/11/2023 |
1.8.7-rc2 | 96 | 9/21/2023 |
1.8.7-rc1 | 82 | 9/12/2023 |
1.8.6 | 123 | 8/31/2023 |
1.8.5 | 759 | 8/25/2023 |
1.8.4 | 118 | 8/24/2023 |
1.8.3 | 121 | 8/23/2023 |
1.8.2 | 200 | 8/22/2023 |
1.8.1 | 137 | 8/18/2023 |
1.8.0 | 117 | 8/15/2023 |
1.7.9 | 155 | 8/11/2023 |
1.7.8 | 101 | 8/11/2023 |
1.7.7 | 146 | 8/10/2023 |
1.7.6 | 130 | 8/9/2023 |
1.7.5 | 186 | 8/9/2023 |
1.7.4 | 222 | 8/3/2023 |
1.7.3 | 138 | 8/1/2023 |
1.7.2 | 125 | 7/31/2023 |
1.7.1 | 128 | 7/27/2023 |
1.7.0 | 137 | 7/25/2023 |
1.6.9 | 129 | 7/25/2023 |
1.6.8 | 123 | 7/24/2023 |
1.6.7 | 143 | 7/20/2023 |
1.6.6 | 149 | 7/19/2023 |
1.6.5 | 103 | 7/19/2023 |
1.6.4 | 123 | 7/17/2023 |
1.6.3 | 108 | 7/17/2023 |
1.6.2 | 161 | 7/12/2023 |
1.6.1 | 155 | 6/30/2023 |
1.6.0 | 95 | 6/30/2023 |