大部分情况下,开发人员遇到的都是维护工作:在现有的代码下,进行维护和新的业务功能开发。
比较少有需要自己全新创建整个 Solution 的情况,所以很多基础的 Dotnet 命令会比较生疏,都是先用先查。

dotnet core 3.1 已经 Release 了,今天尝试一下 BlazorApp 模板,并结合使用 EntityFramework 和 Postgresql + Sqlite 两种 DB。记录一下 dotnet 的系列命令。

需求计划

  1. 创建标准的 Solution 目录结构。

    BlazorApp2
    --src/BlazorApp
    --test/BlazorApp.UnitTest
    --BlazorApp2.sln
  2. 使用 Dapper 从 Postgresql 读取数据,并实时显示日志信息。

  3. 使用 CodeFirst 创建 Sqlite DB。 并完成数据的 增删改查 功能。

准备工作

  1. Postgresql 环境,使用 Docker stack 搭建。

    # 启动命令 docker stack deploy postgres -c stack.yml
    # 执行的时候可能会需要先执行 `docker swarm init`

    # 访问 localhost:8080
    # user/password 是 postgres/postgres

    version: "3.7"

    services:

    postgres:
    image: postgres
    restart: always
    ports:
    - "5432"
    environment:
    POSTGRES_PASSWORD: postgres
    volumes:
    - "postgres-data:/var/lib/postgresql/data"
    networks:
    - app_net

    adminer:
    image: adminer
    restart: always
    ports:
    - 8080:8080
    networks:
    - app_net

    networks:
    app_net:

    volumes:
    postgres-data:

创建标准 Solution 目录结构

  1. mkdir BlazorApp2

  2. dotnet new sln

  3. mkdir src

  4. dotnet new blazorserver -o src/BlazorApp

  5. dotnet sln BlazorApp2.sln add src/BlazorApp/BlazorApp.csproj

  6. mkdir test

  7. dotnet new mstest -o test/BlazorApp.UnitTest

  8. dotnet sln BlazorApp2.sln add test/BlazorApp.UnitTest/BlazorApp.UnitTest.csproj

  9. 代码覆盖率报告怎么加?

  10. dotnet build

  11. dotnet test

  12. dotnet run –project src/BlazorApp/BlazorApp.csproj

Dapper 读取 Porstgresql 数据

  1. cd src/BlazorApp

  2. dotnet add package Dapper –version 2.0.30

  3. dotnet add package Npgsql

  4. add ConnectionStrings in appsettings.json

  5. add Data/SyncDataService.cs

    using Dapper;
    using Npgsql;

    namespace BlazorApp.Data
    {
    public class SyncDataService
    {
    private readonly IConfiguration _configuration;

    public SyncDataService(IConfiguration configuration)
    {
    _configuration = configuration;
    }

    public void SyncData()
    {
    string connStr = _configuration.GetConnectionString("ReportDB");
    using (NpgsqlConnection conn = new NpgsqlConnection(connStr))
    {
    conn.Open();

    string sql = "select id, report from report limit 5";
    var res = conn.Query<Report>(sql);

    conn.Close();
    }
    }
    }
    }

  6. add Pages/SyncData.razor

    @page "/SyncData"

    @using BlazorApp.Data
    @inject SyncDataService _syncDataService

    <h1>SyncData</h1>
    <hr />

    @if (_syncDataService.Reports != null && _syncDataService.Reports.Any())
    {
    <table class="table">
    <thead>
    <tr>
    <th>Id</th>
    <th>Report</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var r in _syncDataService.Reports)
    {
    <tr>
    <td>@r.id</td>
    <td>@r.report</td>
    </tr>
    }
    </tbody>
    </table>
    }

    @code {
    protected override Task OnInitializedAsync()
    {
    _syncDataService.Reports();
    }
    }

  7. Registry service services.AddScoped<SyncDataService>(); in Startup.ConfigureServices

  8. Update Shared/NavMenu.razor file.

  9. dotnet run

DBFirst with Porstgresql

  1. dotnet add package Microsoft.EntityFrameworkCore

  2. dotnet add package Microsoft.EntityFrameworkCore.Tools

  3. dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL

    using Microsoft.EntityFrameworkCore;
    namespace BlazorApp.Data
    {
    public class ScanReportDBContext : DbContext
    {
    public ScanReportDBContext(DbContextOptions<ScanReportDBContext> options) : base(options)
    {

    }

    public DbSet<ScanReport> scanReport { get; set; }
    }
    }
  4. dotnet tool install –global dotnet-ef

  5. dotnet add package Microsoft.EntityFrameworkCore.Design

  6. dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL.Design

  7. dotnet ef dbcontext scaffold “Data Source=xxxx” Npgsql.EntityFrameworkCore.PostgreSQL –context-dir Data –output-dir Models

Code First with Sqlite

  1. dotnet add package Microsoft.EntityFrameworkCore

  2. dotnet add package Microsoft.EntityFrameworkCore.Tools

  3. dotnet add package Microsoft.EntityFrameworkCore.Sqlite

  4. dotnet tool install –global dotnet-ef

  5. dotnet ef migrations add InitMigration

  6. dotnet ef database update

Using MemoryCache

using Microsoft.Extensions.Caching.Memory;

namespace BlazorApp2.Data
{
public class ScanReportService
{
private const string cached_scan_reports_key = "cached_scan_reports";
private readonly IMemoryCache _memoryCache;
private readonly ScanReportContext _scanReportContext;
public ScanReportService(IMemoryCache memoryCache, scanReportContext scanReportContext)
{
_memoryCache = memoryCache;
_scanReportContext = scanReportContext;
}
public List<Report> SyncScanReportToCache()
{
List<Report> cached_scan_reports;
if (!_memoryCache.TryGetValue(cached_scan_reports_key, out cached_scan_reports))
{
cached_scan_reports = new List<Report>();
}else
{
// cached_scan_reports = _scanReportContext.Report.ToList();
_memoryCache.Set(cached_scan_reports_key, cached_scan_reports);
}
return cached_scan_reports;
}
}
}

文档

  1. https://www.npgsql.org/efcore/
  2. https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet