EasilyNET.Mongo.AspNetCore 1.9.0-preview1

This is a prerelease version of EasilyNET.Mongo.AspNetCore.
There is a newer version of this package available.
See the version list below for details.
dotnet add package EasilyNET.Mongo.AspNetCore --version 1.9.0-preview1
                    
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 1.9.0-preview1
                    
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="1.9.0-preview1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EasilyNET.Mongo.AspNetCore" Version="1.9.0-preview1" />
                    
Directory.Packages.props
<PackageReference Include="EasilyNET.Mongo.AspNetCore" />
                    
Project file
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 1.9.0-preview1
                    
#r "nuget: EasilyNET.Mongo.AspNetCore, 1.9.0-preview1"
                    
#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.
#addin nuget:?package=EasilyNET.Mongo.AspNetCore&version=1.9.0-preview1&prerelease
                    
Install EasilyNET.Mongo.AspNetCore as a Cake Addin
#tool nuget:?package=EasilyNET.Mongo.AspNetCore&version=1.9.0-preview1&prerelease
                    
Install EasilyNET.Mongo.AspNetCore as a Cake Tool

EasilyNET.Mongo.AspNetCore

  • 一个 MongoDB 驱动的服务包,方便使用 MongoDB 数据库.
  • 数据库中字段名驼峰命名,ID,Id 自动转化成 ObjectId.
  • 可配置部分类的 Id 字段不存为 ObjectId,而存为 string 类型.
  • 自动本地化 MongoDB 时间类型
  • 添加.Net6 Date/Time Only 类型支持(TimeOnly 理论上应该是兼容原 TimeSpan 数据类型).
  • 添加 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)
    };
    // 是否使用HoyoMongo的一些默认转换配置.包含如下内容:
    // 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().RegisterDynamicSerializer();
// 注册别的序列化方案
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)
        //    };
        //    // 是否使用HoyoMongo的一些默认转换配置.包含如下内容:
        //    // 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));
        //context.Services.RegisterSerializer().RegisterDynamicSerializer();

        // 例子二:使用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)
            };
            // 是否使用HoyoMongo的一些默认转换配置.包含如下内容:
            // 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) }
                }
            };
        });
        context.Services.AddMongoContext<DbContext2>(config, c =>
        {
            c.DefaultConventionRegistry = true;
            c.ConventionRegistry = new()
            {
                {
                    $"{SnowId.GenerateNewId()}",
                    new() { new IgnoreIfDefaultConvention(true) }
                }
            };
        });
        //context.Services.RegisterSerializer().RegisterDynamicSerializer();
    }
}
  • 创建 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 Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.411.142 133 4/11/2025
4.25.409.92 154 4/9/2025
4.25.403.133 147 4/3/2025
4.25.319.113 138 3/19/2025
4.25.312.103 162 3/12/2025
4.25.227.135 104 2/27/2025
4.25.221.115 99 2/21/2025
4.25.212.95 253 2/12/2025
4.25.211.140 106 2/11/2025
4.25.124.223 86 1/24/2025
4.25.116.110 105 1/16/2025
4.25.115.121 62 1/15/2025
4.25.114.172 73 1/14/2025
4.25.109.111 73 1/9/2025
4.25.108.182 80 1/8/2025
4.25.108.160 83 1/8/2025
4.25.1.1 107 1/1/2025
3.24.1224.141 94 12/24/2024
3.24.1216.116 114 12/16/2024
3.24.1206.100 101 12/6/2024
3.24.1205.171 114 12/5/2024
3.24.1202.150 93 12/2/2024
3.24.1126.231 93 11/26/2024
3.24.1126.172 103 11/26/2024
3.24.1126.114 101 11/26/2024
3.24.1126.104 99 11/26/2024
3.24.1125.181 96 11/25/2024
3.24.1125.104 104 11/25/2024
3.24.1121.183 95 11/21/2024
3.24.1120.183 99 11/20/2024
3.24.1119.31 99 11/18/2024
3.24.1115.143 91 11/15/2024
3.24.1113.100 94 11/13/2024
3.24.1112.125 590 11/12/2024
3.24.1107.140 106 11/7/2024
3.24.1107.54 104 11/7/2024
3.24.1107.34 98 11/7/2024
3.24.1105.111 102 11/5/2024
3.24.1103.31 96 11/2/2024
3.24.1103 103 11/2/2024
3.24.1031.135 99 10/31/2024
3.24.1031.112 92 10/31/2024
3.24.1031.104 98 10/31/2024
3.24.1029.142 99 10/29/2024
3.24.1025.30 203 10/24/2024
3.24.1022.142 87 10/22/2024
3.24.1018.204 154 10/18/2024
3.24.1018.175 141 10/18/2024
3.24.1018.166 145 10/18/2024
3.24.1018.93 147 10/18/2024
3.24.1017.42 111 10/16/2024
3.24.1016.161 110 10/16/2024
3.24.1015.231 95 10/15/2024
3.24.1015.14 96 10/14/2024
3.24.1012.114 102 10/12/2024
3.24.1009.115 105 10/9/2024
3.24.1008.160 92 10/8/2024
3.24.1008.133 98 10/8/2024
3.24.1007.185 99 10/7/2024
3.24.1003.33 110 10/2/2024
3.24.1002.162 108 10/2/2024
3.24.929.143 243 9/29/2024
3.24.929.141 104 9/29/2024
3.24.929.131 79 9/29/2024
3.24.929.122 70 9/29/2024
3.24.926.184 75 9/26/2024
3.24.926.182 71 9/26/2024
3.24.926.175 72 9/26/2024
3.24.924.160 78 9/24/2024
3.24.924.133 79 9/24/2024
3.24.924.124 65 9/24/2024
3.24.924.10 74 9/23/2024
3.24.924.1 74 9/23/2024
3.24.923.234 68 9/23/2024
3.24.923.232 71 9/23/2024
3.24.923.155 71 9/23/2024
3.24.919.92 73 9/19/2024
3.24.914.125 241 9/14/2024
3.24.914.115 93 9/14/2024
3.24.914.111 71 9/14/2024
3.24.911.95 73 9/11/2024
3.24.908.215 67 9/8/2024
3.24.904.200 220 9/4/2024
3.24.828.163 77 8/28/2024
3.24.820.173 77 8/20/2024
3.24.814.92 88 8/14/2024
3.24.812.115 79 8/12/2024
3.24.802.100 58 8/2/2024
3.24.801.162 65 8/1/2024
3.24.801.160 64 8/1/2024
3.24.801.155 66 8/1/2024
3.24.730.164 56 7/30/2024
3.24.730.91 58 7/30/2024
3.24.724.91 59 7/24/2024
3.24.718.105 74 7/18/2024
3.24.716.95 70 7/16/2024
3.24.712.94 69 7/12/2024
3.24.710.14 69 7/9/2024
3.24.709.105 69 7/9/2024
3.24.704.94 74 7/4/2024
3.24.701.90 74 7/1/2024
3.24.628.114 81 6/28/2024
3.24.627.145 67 6/27/2024
3.24.620.160 76 6/20/2024
3.24.613.115 67 6/13/2024
3.24.612.95 69 6/12/2024
3.24.528.90 71 5/28/2024
3.24.522.84 80 5/22/2024
3.24.512.213 76 5/12/2024
3.24.508.112 91 5/8/2024
2.2024.428.71 71 4/28/2024
2.2024.427.1128 73 4/27/2024
2.2.72 145 4/14/2024
2.2.71 84 4/12/2024
2.2.8 68 4/26/2024
2.2.6 82 4/10/2024
2.2.5 90 3/26/2024
2.2.4 89 3/25/2024
2.2.3 85 3/24/2024
2.2.2 86 3/21/2024
2.2.1 86 3/20/2024
2.2.0 87 3/13/2024
2.1.9 92 2/21/2024
2.1.8 80 2/18/2024
2.1.7 88 2/16/2024
2.1.6 106 2/14/2024
2.1.5 84 2/14/2024
2.1.4 163 2/9/2024
2.1.3 131 2/8/2024
2.1.2 153 2/5/2024
2.1.1.2 206 12/26/2023
2.1.1.1 132 12/26/2023
2.1.1 138 12/25/2023
2.1.0 173 12/17/2023
2.0.11 202 12/6/2023
2.0.1 207 11/15/2023
2.0.0 151 11/14/2023
1.9.1 123 11/1/2023
1.9.0 114 10/19/2023
1.9.0-preview2 311 10/12/2023
1.9.0-preview1 80 10/12/2023
1.8.9 153 10/11/2023
1.8.8 131 10/11/2023
1.8.7-rc2 104 9/21/2023
1.8.7-rc1 90 9/12/2023
1.8.6 132 8/31/2023
1.8.5 807 8/25/2023
1.8.4 126 8/24/2023
1.8.3 130 8/23/2023
1.8.2 208 8/22/2023
1.8.1 146 8/18/2023
1.8.0 126 8/15/2023
1.7.9 165 8/11/2023
1.7.8 109 8/11/2023
1.7.7 157 8/10/2023
1.7.6 139 8/9/2023
1.7.5 195 8/9/2023
1.7.4 230 8/3/2023
1.7.3 147 8/1/2023
1.7.2 134 7/31/2023
1.7.1 138 7/27/2023
1.7.0 145 7/25/2023
1.6.9 138 7/25/2023
1.6.8 133 7/24/2023
1.6.7 151 7/20/2023
1.6.6 159 7/19/2023
1.6.5 114 7/19/2023
1.6.4 131 7/17/2023
1.6.3 116 7/17/2023
1.6.2 171 7/12/2023
1.6.1 168 6/30/2023
1.6.0 106 6/30/2023