Abner.Extensions.Configuration.Db.Sqlite 1.0.0

dotnet add package Abner.Extensions.Configuration.Db.Sqlite --version 1.0.0                
NuGet\Install-Package Abner.Extensions.Configuration.Db.Sqlite -Version 1.0.0                
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="Abner.Extensions.Configuration.Db.Sqlite" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Abner.Extensions.Configuration.Db.Sqlite --version 1.0.0                
#r "nuget: Abner.Extensions.Configuration.Db.Sqlite, 1.0.0"                
#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 Abner.Extensions.Configuration.Db.Sqlite as a Cake Addin
#addin nuget:?package=Abner.Extensions.Configuration.Db.Sqlite&version=1.0.0

// Install Abner.Extensions.Configuration.Db.Sqlite as a Cake Tool
#tool nuget:?package=Abner.Extensions.Configuration.Db.Sqlite&version=1.0.0                

Abner.Extensions.Configuration.DbProvider

扩展.net core配置框架,从数据库中读取配置文件(目前支持的数据库 mysql,sqlserver,sqlite)

第一步

以mysql为例,环境以vs2022+.net6为例,通过nuget安装包

Install-Package Abner.Extensions.Configuration.Db.MySql
第二步

打开appsettings.json文件添加以下节点

"ConnectionStrings": {
    "MySql": "Server=localhost;Port=3306;Database=Demo;User=root;Password=123456;",
    "SqlServer": "Data Source=(localdb)\\ProjectModels;Initial Catalog=demo;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
    "Sqlite": "Data Source=configuration.db;Cache=Shared"
  }

需先在指定的dbms中创建连接字符串中的数据库“Demo”,使用sqlite时可忽略

在program中添加以下代码即可

using Abner.Extensions.Configuration.Db.MySql;

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddMySql(builder.Configuration.GetConnectionString("MySql"));
builder.Services.AddControllers();

var app = builder.Build();
app.MapControllers();
app.Run();
第三步

通过运行程序,会在指定的数据库中创建默认的配置表“Db_Configs”,有三个表字段,默认为Id,Name,Value, 其中Id为自增主键,也可通过以下代码进行配置表名跟表字段;

builder.Configuration.AddMySql(options =>
{
    options.DbSetting.ConnectionString = builder.Configuration.GetConnectionString("MySql");
    options.DbSetting.TableName = "YourTableName";
    options.DbSetting.KeyColumn = "YourKeyField";
    options.DbSetting.ValueColumn = "YourValueField";
});

也可自行在数据库中创建对应的表,配置时按照手动创建表时指定的名称即可;

可以支持以下形式的value:

image-20220324150357879

第四步

在Controller中添加以下代码:

private readonly ILogger<WeatherForecastController> _logger;
private readonly IOptions<DemoOption> _demoOption;
private readonly IConfiguration _configuration;

public WeatherForecastController(ILogger<WeatherForecastController> logger, 
                                 IOptions<DemoOption> demoOption,
                                 IConfiguration configuration)
{
    _logger = logger;
    this._demoOption = demoOption;
    this._configuration = configuration;
}

[HttpGet]
public IActionResult Get()
{
    return new JsonResult(new
                          {
                              json = _demoOption.Value,
                              array = _configuration["MySqlArray:0:myKey"],
                              text = _configuration["MysqlText"]
                          });
}

在浏览器中访问时返回

image-20220324150429587

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
1.0.0 300 3/24/2022