EasilyNET.Mongo.AspNetCore 1.7.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasilyNET.Mongo.AspNetCore --version 1.7.3                
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 1.7.3                
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.7.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.Mongo.AspNetCore --version 1.7.3                
#r "nuget: EasilyNET.Mongo.AspNetCore, 1.7.3"                
#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=1.7.3

// Install EasilyNET.Mongo.AspNetCore as a Cake Tool
#tool nuget:?package=EasilyNET.Mongo.AspNetCore&version=1.7.3                

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. 
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
3.24.1126.231 66 11/26/2024
3.24.1126.172 73 11/26/2024
3.24.1126.114 71 11/26/2024
3.24.1126.104 62 11/26/2024
3.24.1125.181 67 11/25/2024
3.24.1125.104 73 11/25/2024
3.24.1121.183 68 11/21/2024
3.24.1120.183 70 11/20/2024
3.24.1119.31 73 11/18/2024
3.24.1115.143 68 11/15/2024
3.24.1113.100 68 11/13/2024
3.24.1112.125 139 11/12/2024
3.24.1107.140 75 11/7/2024
3.24.1107.54 71 11/7/2024
3.24.1107.34 65 11/7/2024
3.24.1105.111 80 11/5/2024
3.24.1103.31 71 11/2/2024
3.24.1103 76 11/2/2024
3.24.1031.135 73 10/31/2024
3.24.1031.112 66 10/31/2024
3.24.1031.104 73 10/31/2024
3.24.1029.142 69 10/29/2024
3.24.1025.30 176 10/24/2024
3.24.1022.142 62 10/22/2024
3.24.1018.204 132 10/18/2024
3.24.1018.175 123 10/18/2024
3.24.1018.166 126 10/18/2024
3.24.1018.93 127 10/18/2024
3.24.1017.42 92 10/16/2024
3.24.1016.161 91 10/16/2024
3.24.1015.231 75 10/15/2024
3.24.1015.14 77 10/14/2024
3.24.1012.114 80 10/12/2024
3.24.1009.115 84 10/9/2024
3.24.1008.160 73 10/8/2024
3.24.1008.133 77 10/8/2024
3.24.1007.185 81 10/7/2024
3.24.1003.33 88 10/2/2024
3.24.1002.162 89 10/2/2024
3.24.929.143 225 9/29/2024
3.24.929.141 85 9/29/2024
3.24.929.131 62 9/29/2024
3.24.929.122 53 9/29/2024
3.24.926.184 55 9/26/2024
3.24.926.182 55 9/26/2024
3.24.926.175 53 9/26/2024
3.24.924.160 58 9/24/2024
3.24.924.133 59 9/24/2024
3.24.924.124 48 9/24/2024
3.24.924.10 54 9/23/2024
3.24.924.1 53 9/23/2024
3.24.923.234 47 9/23/2024
3.24.923.232 52 9/23/2024
3.24.923.155 52 9/23/2024
3.24.919.92 54 9/19/2024
3.24.914.125 226 9/14/2024
3.24.914.115 76 9/14/2024
3.24.914.111 58 9/14/2024
3.24.911.95 60 9/11/2024
3.24.908.215 54 9/8/2024
3.24.904.200 207 9/4/2024
3.24.828.163 64 8/28/2024
3.24.820.173 65 8/20/2024
3.24.814.92 72 8/14/2024
3.24.812.115 64 8/12/2024
3.24.802.100 42 8/2/2024
3.24.801.162 53 8/1/2024
3.24.801.160 50 8/1/2024
3.24.801.155 51 8/1/2024
3.24.730.164 41 7/30/2024
3.24.730.91 41 7/30/2024
3.24.724.91 44 7/24/2024
3.24.718.105 59 7/18/2024
3.24.716.95 55 7/16/2024
3.24.712.94 53 7/12/2024
3.24.710.14 55 7/9/2024
3.24.709.105 52 7/9/2024
3.24.704.94 58 7/4/2024
3.24.701.90 58 7/1/2024
3.24.628.114 65 6/28/2024
3.24.627.145 51 6/27/2024
3.24.620.160 58 6/20/2024
3.24.613.115 52 6/13/2024
3.24.612.95 55 6/12/2024
3.24.528.90 59 5/28/2024
3.24.522.84 65 5/22/2024
3.24.512.213 62 5/12/2024
3.24.508.112 76 5/8/2024
2.2024.428.71 61 4/28/2024
2.2024.427.1128 62 4/27/2024
2.2.72 135 4/14/2024
2.2.71 72 4/12/2024
2.2.8 59 4/26/2024
2.2.6 66 4/10/2024
2.2.5 80 3/26/2024
2.2.4 80 3/25/2024
2.2.3 75 3/24/2024
2.2.2 75 3/21/2024
2.2.1 74 3/20/2024
2.2.0 77 3/13/2024
2.1.9 79 2/21/2024
2.1.8 67 2/18/2024
2.1.7 73 2/16/2024
2.1.6 91 2/14/2024
2.1.5 70 2/14/2024
2.1.4 150 2/9/2024
2.1.3 116 2/8/2024
2.1.2 142 2/5/2024
2.1.1.2 196 12/26/2023
2.1.1.1 122 12/26/2023
2.1.1 128 12/25/2023
2.1.0 163 12/17/2023
2.0.11 197 12/6/2023
2.0.1 199 11/15/2023
2.0.0 145 11/14/2023
1.9.1 117 11/1/2023
1.9.0 106 10/19/2023
1.9.0-preview2 279 10/12/2023
1.9.0-preview1 75 10/12/2023
1.8.9 147 10/11/2023
1.8.8 126 10/11/2023
1.8.7-rc2 97 9/21/2023
1.8.7-rc1 83 9/12/2023
1.8.6 126 8/31/2023
1.8.5 762 8/25/2023
1.8.4 121 8/24/2023
1.8.3 124 8/23/2023
1.8.2 203 8/22/2023
1.8.1 140 8/18/2023
1.8.0 120 8/15/2023
1.7.9 158 8/11/2023
1.7.8 104 8/11/2023
1.7.7 149 8/10/2023
1.7.6 133 8/9/2023
1.7.5 189 8/9/2023
1.7.4 225 8/3/2023
1.7.3 141 8/1/2023
1.7.2 128 7/31/2023
1.7.1 131 7/27/2023
1.7.0 140 7/25/2023
1.6.9 132 7/25/2023
1.6.8 126 7/24/2023
1.6.7 146 7/20/2023
1.6.6 152 7/19/2023
1.6.5 106 7/19/2023
1.6.4 126 7/17/2023
1.6.3 111 7/17/2023
1.6.2 164 7/12/2023
1.6.1 158 6/30/2023
1.6.0 98 6/30/2023