EasilyNET.Mongo.AspNetCore 4.25.319.113

dotnet add package EasilyNET.Mongo.AspNetCore --version 4.25.319.113                
NuGet\Install-Package EasilyNET.Mongo.AspNetCore -Version 4.25.319.113                
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="4.25.319.113" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasilyNET.Mongo.AspNetCore --version 4.25.319.113                
#r "nuget: EasilyNET.Mongo.AspNetCore, 4.25.319.113"                
#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=4.25.319.113

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

EasilyNET.Mongo.AspNetCore

  • 一个 MongoDB 驱动的服务包,方便使用 MongoDB 数据库.
  • 数据库中字段名驼峰命名,ID,Id 自动转化成 ObjectId.
  • 可配置部分类的 Id 字段不存为 ObjectId,而存为 string 类型.支持子对象以及集合成员的 Id 字段转化.
  • 自动本地化 MongoDB 时间类型
  • 添加.Net6 Date/Time Only 类型支持(序列化到 String 或 long)

ChangeLogs
  • 支持自定义 TimeOnly 和 DateOnly 的格式化格式.
    1. 支持转换成字符串格式
    2. 转换成 Ticks 的方式存储
    3. 若想转化成其他类型也可自行实现,如:转化成 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 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.  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.  net10.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
4.25.319.113 76 3 days ago
4.25.312.103 149 10 days ago
4.25.227.135 94 23 days ago
4.25.221.115 89 a month ago
4.25.212.95 244 a month ago
4.25.211.140 98 a month ago
4.25.124.223 78 2 months ago
4.25.116.110 92 2 months ago
4.25.115.121 55 2 months ago
4.25.114.172 64 2 months ago
4.25.109.111 67 2 months ago
4.25.108.182 73 2 months ago
4.25.108.160 76 2 months ago
4.25.1.1 100 3 months ago
3.24.1224.141 87 3 months ago
3.24.1216.116 103 3 months ago
3.24.1206.100 95 4 months ago
3.24.1205.171 108 4 months ago
3.24.1202.150 86 4 months ago
3.24.1126.231 87 4 months ago
3.24.1126.172 97 4 months ago
3.24.1126.114 94 4 months ago
3.24.1126.104 93 4 months ago
3.24.1125.181 89 4 months ago
3.24.1125.104 96 4 months ago
3.24.1121.183 88 4 months ago
3.24.1120.183 91 4 months ago
3.24.1119.31 92 4 months ago
3.24.1115.143 86 4 months ago
3.24.1113.100 89 4 months ago
3.24.1112.125 584 4 months ago
3.24.1107.140 101 4 months ago
3.24.1107.54 99 4 months ago
3.24.1107.34 92 4 months ago
3.24.1105.111 97 5 months ago
3.24.1103.31 90 5 months ago
3.24.1103 97 5 months ago
3.24.1031.135 94 5 months ago
3.24.1031.112 87 5 months ago
3.24.1031.104 93 5 months ago
3.24.1029.142 94 5 months ago
3.24.1025.30 197 5 months ago
3.24.1022.142 82 5 months ago
3.24.1018.204 148 5 months ago
3.24.1018.175 135 5 months ago
3.24.1018.166 140 5 months ago
3.24.1018.93 141 5 months ago
3.24.1017.42 106 5 months ago
3.24.1016.161 105 5 months ago
3.24.1015.231 90 5 months ago
3.24.1015.14 91 5 months ago
3.24.1012.114 96 5 months ago
3.24.1009.115 99 5 months ago
3.24.1008.160 86 5 months ago
3.24.1008.133 92 5 months ago
3.24.1007.185 94 5 months ago
3.24.1003.33 104 6 months ago
3.24.1002.162 102 6 months ago
3.24.929.143 238 6 months ago
3.24.929.141 98 6 months ago
3.24.929.131 73 6 months ago
3.24.929.122 64 6 months ago
3.24.926.184 69 6 months ago
3.24.926.182 65 6 months ago
3.24.926.175 66 6 months ago
3.24.924.160 72 6 months ago
3.24.924.133 74 6 months ago
3.24.924.124 59 6 months ago
3.24.924.10 69 6 months ago
3.24.924.1 68 6 months ago
3.24.923.234 62 6 months ago
3.24.923.232 66 6 months ago
3.24.923.155 66 6 months ago
3.24.919.92 68 6 months ago
3.24.914.125 236 6 months ago
3.24.914.115 87 6 months ago
3.24.914.111 66 6 months ago
3.24.911.95 68 6 months ago
3.24.908.215 61 6 months ago
3.24.904.200 215 7 months ago
3.24.828.163 72 7 months ago
3.24.820.173 72 7 months ago
3.24.814.92 82 7 months ago
3.24.812.115 73 7 months ago
3.24.802.100 51 8 months ago
3.24.801.162 60 8 months ago
3.24.801.160 58 8 months ago
3.24.801.155 60 8 months ago
3.24.730.164 51 8 months ago
3.24.730.91 52 8 months ago
3.24.724.91 53 8 months ago
3.24.718.105 69 8 months ago
3.24.716.95 64 8 months ago
3.24.712.94 63 8 months ago
3.24.710.14 64 8 months ago
3.24.709.105 64 8 months ago
3.24.704.94 68 9 months ago
3.24.701.90 68 9 months ago
3.24.628.114 75 9 months ago
3.24.627.145 61 9 months ago
3.24.620.160 70 9 months ago
3.24.613.115 62 9 months ago
3.24.612.95 65 9 months ago
3.24.528.90 66 10 months ago
3.24.522.84 75 10 months ago
3.24.512.213 71 5/12/2024
3.24.508.112 87 5/8/2024
2.2024.428.71 66 4/28/2024
2.2024.427.1128 69 4/27/2024
2.2.72 140 4/14/2024
2.2.71 79 4/12/2024
2.2.8 63 4/26/2024
2.2.6 78 4/10/2024
2.2.5 86 3/26/2024
2.2.4 85 3/25/2024
2.2.3 80 3/24/2024
2.2.2 81 3/21/2024
2.2.1 81 3/20/2024
2.2.0 83 3/13/2024
2.1.9 88 2/21/2024
2.1.8 76 2/18/2024
2.1.7 84 2/16/2024
2.1.6 101 2/14/2024
2.1.5 79 2/14/2024
2.1.4 159 2/9/2024
2.1.3 127 2/8/2024
2.1.2 148 2/5/2024
2.1.1.2 202 12/26/2023
2.1.1.1 128 12/26/2023
2.1.1 133 12/25/2023
2.1.0 168 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 122 11/1/2023
1.9.0 113 10/19/2023
1.9.0-preview2 311 10/12/2023
1.9.0-preview1 80 10/12/2023
1.8.9 152 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 805 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 145 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