EasilyNET.Mongo.AspNetCore 2.2.72

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasilyNET.Mongo.AspNetCore --version 2.2.72
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 2.2.72
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.72" />
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.72
#r "nuget: EasilyNET.Mongo.AspNetCore, 2.2.72"
#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.72

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

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 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.

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.508.112 30 5/8/2024
2.2024.428.71 40 4/28/2024
2.2024.427.1128 45 4/27/2024
2.2.72 111 4/14/2024
2.2.71 46 4/12/2024
2.2.8 38 4/26/2024
2.2.6 48 4/10/2024
2.2.5 54 3/26/2024
2.2.4 63 3/25/2024
2.2.3 55 3/24/2024
2.2.2 58 3/21/2024
2.2.1 55 3/20/2024
2.2.0 58 3/13/2024
2.1.9 59 2/21/2024
2.1.8 51 2/18/2024
2.1.7 54 2/16/2024
2.1.6 75 2/14/2024
2.1.5 53 2/14/2024
2.1.4 105 2/9/2024
2.1.3 90 2/8/2024
2.1.2 106 2/5/2024
2.1.1.2 148 12/26/2023
2.1.1.1 73 12/26/2023
2.1.1 82 12/25/2023
2.1.0 111 12/17/2023
2.0.11 165 12/6/2023
2.0.1 169 11/15/2023
2.0.0 106 11/14/2023
1.9.1 93 11/1/2023
1.9.0 82 10/19/2023
1.9.0-preview2 214 10/12/2023
1.9.0-preview1 61 10/12/2023
1.8.9 123 10/11/2023
1.8.8 107 10/11/2023
1.8.7-rc2 81 9/21/2023
1.8.7-rc1 70 9/12/2023
1.8.6 103 8/31/2023
1.8.5 170 8/25/2023
1.8.4 101 8/24/2023
1.8.3 109 8/23/2023
1.8.2 184 8/22/2023
1.8.1 120 8/18/2023
1.8.0 107 8/15/2023
1.7.9 142 8/11/2023
1.7.8 89 8/11/2023
1.7.7 128 8/10/2023
1.7.6 119 8/9/2023
1.7.5 175 8/9/2023
1.7.4 205 8/3/2023
1.7.3 127 8/1/2023
1.7.2 110 7/31/2023
1.7.1 109 7/27/2023
1.7.0 120 7/25/2023
1.6.9 111 7/25/2023
1.6.8 111 7/24/2023
1.6.7 126 7/20/2023
1.6.6 129 7/19/2023
1.6.5 92 7/19/2023
1.6.4 109 7/17/2023
1.6.3 90 7/17/2023
1.6.2 149 7/12/2023
1.6.1 144 6/30/2023
1.6.0 84 6/30/2023