Aiursoft.DbTools.Switchable 9.0.11

DBTools

MIT licensed Pipeline stat Test Coverage NuGet version (Aiursoft.CSTools) ManHours

DbTools are Aiursoft's common database tools. It simplifies the process of registering DbContext and updating database. So your application can easily switch from different database types.

Installation

To install Aiursoft.DbTools to your project from nuget.org:

dotnet add package Aiursoft.DbTools

Project structure

---
title: Project dependency diagram
---

stateDiagram-v2
    Aiursoft.DbTools.InMemory --> Aiursoft.DbTools
    Aiursoft.DbTools.Sqlite --> Aiursoft.DbTools
    Aiursoft.DbTools.MySql --> Aiursoft.DbTools
    Aiursoft.DbTools.Switchable --> Aiursoft.DbTools
    Demo.Entities --> Aiursoft.DbTools
    Demo.Sqlite --> Aiursoft.DbTools.Sqlite
    Demo.Sqlite --> Demo.Entities
    Demo.MySql --> Aiursoft.DbTools.MySql
    Demo.MySql --> Demo.Entities
    Demo.InMemory --> Aiursoft.DbTools.InMemory
    Demo.InMemory --> Demo.Entities
    Demo.WebApp --> Aiursoft.DbTools.Switchable
    Demo.WebApp --> Demo.InMemory
    Demo.WebApp --> Demo.MySql
    Demo.WebApp --> Demo.Sqlite
    Aiursoft.DbTools.SqlServer --> Aiursoft.DbTools

Usage

In your startup.cs:

public void ConfigureServices(IConfiguration configuration, IWebHostEnvironment environment, IServiceCollection services)
{
    var (connectionString, dbType, allowCache) = configuration.GetDbSettings();
    services.AddSwitchableRelationalDatabase(
        dbType: EntryExtends.IsInUnitTests() ? "InMemory": dbType,
        connectionString: connectionString,
        supportedDbs:
        [
            new MySqlSupportedDb(allowCache: allowCache, splitQuery: false),
            new SqliteSupportedDb(allowCache: allowCache, splitQuery: true),
            new InMemorySupportedDb()
        ]);

    services
        .AddControllersWithViews()
        .AddApplicationPart(typeof(Startup).Assembly);
}

In your appsettings.json:

{
    "ConnectionStrings": {
        "AllowCache": "True",
        "DbType": "Sqlite",
        "DefaultConnection": "DataSource=app.db;Cache=Shared"
    }
}

Or:

{
  // sudo docker run -d --name db -e MYSQL_RANDOM_ROOT_PASSWORD=true -e MYSQL_DATABASE=kahla -e MYSQL_USER=kahla -e MYSQL_PASSWORD=kahla_password -p 3306:3306 hub.aiursoft.cn/mysql
  "ConnectionStrings": {
    "AllowCache": "True",
    "DbType": "MySql",
    "DefaultConnection": "Server=localhost;Database=kahla;Uid=kahla;Pwd=kahla_password;"
  }
}

Or:

{
  "ConnectionStrings": {
    "AllowCache": "False",
    "DbType": "InMemory"
  }
}

Simple, isn't it?

Your database project need to be different with your web project, you need the following command to generate migrations:

cd ./Demo.MySql
dotnet ef migrations add Init --context "MySqlContext" -s ../Demo.WebApp/Demo.WebApp.csproj
cd ..

cd ./Demo.Sqlite
dotnet ef migrations add Init --context "SqliteContext" -s ../Demo.WebApp/Demo.WebApp.csproj
cd ..

For more usage, please check the Demo app in the demos folder!

How to contribute

There are many ways to contribute to the project: logging bugs, submitting pull requests, reporting issues, and creating suggestions.

Even if you with push rights on the repository, you should create a personal fork and create feature branches there when you need them. This keeps the main repository clean and your workflow cruft out of sight.

We're also interested in your feedback on the future of this project. You can submit a suggestion or feature request through the issue tracker. To make this process more effective, we're asking that these include more information to help define them more clearly.

No packages depend on Aiursoft.DbTools.Switchable.

.NET 9.0

Version Downloads Last updated
9.0.12 0 02/27/2025
9.0.11 0 02/27/2025
9.0.10 0 02/27/2025
9.0.9 0 02/25/2025
9.0.8 0 02/23/2025
9.0.7 0 02/12/2025
9.0.6 47 02/01/2025
9.0.5 42 01/16/2025
9.0.4 6 01/15/2025
9.0.3 6 01/15/2025
9.0.1 6 01/14/2025
9.0.0 14 12/30/2024
8.0.27 34 12/11/2024
8.0.26 33 11/14/2024
8.0.25 7 11/13/2024
8.0.24 1 11/11/2024
8.0.23 14 11/09/2024
8.0.22 25 10/26/2024
8.0.21 12 10/15/2024
8.0.20 4 10/14/2024
8.0.19 7 10/09/2024
8.0.18 6 10/07/2024
8.0.17 9 09/29/2024
8.0.16 2 09/28/2024
8.0.15 7 09/22/2024
8.0.14 35 08/22/2024
8.0.13 5 08/18/2024
8.0.12 40 07/23/2024
8.0.11 36 07/10/2024
8.0.10 9 07/07/2024
8.0.9 24 06/29/2024
8.0.8 24 06/05/2024
8.0.7 27 05/28/2024
8.0.6 22 05/16/2024
8.0.5 39 05/04/2024
8.0.4 36 04/06/2024
8.0.3 30 03/31/2024
8.0.2 12 03/31/2024
8.0.1 19 03/26/2024
8.0.0 7 03/25/2024