Net7.MongoDBHelper
1.0.1
dotnet add package Net7.MongoDBHelper --version 1.0.1
NuGet\Install-Package Net7.MongoDBHelper -Version 1.0.1
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="Net7.MongoDBHelper" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Net7.MongoDBHelper" Version="1.0.1" />
<PackageReference Include="Net7.MongoDBHelper" />
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 Net7.MongoDBHelper --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Net7.MongoDBHelper, 1.0.1"
#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.
#:package Net7.MongoDBHelper@1.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Net7.MongoDBHelper&version=1.0.1
#tool nuget:?package=Net7.MongoDBHelper&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MongoDBHelper
介绍
MongoDB工具
软件架构
软件架构说明
工具注意点
1、部分函数要求模型需要字段有主键比如字段带有[BsonId]特性或者字段里包含_id;
2、由于官方驱动 有一个单独处理附件类驱动,所以本项目封装时按照数据集合(DataContext) 和 附件集合(FileContext);
3、DataContext封装范围是某个数据库,FileContext封装范围是某个数据库下的某个文件集合;
使用说明
一、依赖注入方式: 1. 只连接一个数据库时: 可以直接使用自带的Context类,数据集合使用DataContext,附件集合使用FileContext,在Program.cs文件里使用
builder.Services.AddMongoDataContext<DataContext>(p =>
{
p.ConnectionString = "mongodb://127.0.0.1:27017";
p.DatabaseName = "TempMongo";
});
只连接一个文件集合时:
builder.Services.AddMongoFileContext<FileContext>(p =>
{
p.ConnectionString = "mongodb://127.0.0.1:27017";
p.DatabaseName = "TempMongo";
p.CollectionName = "Files";
});
然后可以在控制器内使用构造注入,比如:
private readonly DataContext _db;
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger, DataContext db)
{
_logger = logger;
_db = db;
}
2. 如果连接多个数据库时:
可以自定义上下文类并集成DataContext或者FileContext,例如:
public class DbContext : DataContext
{
/// <summary>
/// 构造函数
/// </summary>
public DbContext()
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="config"></param>
public DbContext(Action<MongoConfig> config) : base(config)
{
}
}
,在Program.cs文件里使用
builder.Services.AddMongoDataContext<DbContext>(p =>
{
p.ConnectionString = "mongodb://127.0.0.1:27017";
p.DatabaseName = "TempMongo";
});
然后可以在控制器内使用构造注入,比如:
private readonly DbContext_db;
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger, DbContext db)
{
_logger = logger;
_db = db;
}
二、普通new实例:
DataContext _db=new DataContext (p =>
{
p.ConnectionString = "mongodb://127.0.0.1:27017";
p.DatabaseName = "TempMongo";
})
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net7.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.1.1)
- Microsoft.AspNetCore.StaticFiles (>= 2.1.1)
- MongoDB.Driver (>= 2.19.1)
- MongoDB.Driver.GridFS (>= 2.19.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.