Perigon.PostgreSQL
0.1.4
dotnet add package Perigon.PostgreSQL --version 0.1.4
NuGet\Install-Package Perigon.PostgreSQL -Version 0.1.4
<PackageReference Include="Perigon.PostgreSQL" Version="0.1.4" />
<PackageVersion Include="Perigon.PostgreSQL" Version="0.1.4" />
<PackageReference Include="Perigon.PostgreSQL" />
paket add Perigon.PostgreSQL --version 0.1.4
#r "nuget: Perigon.PostgreSQL, 0.1.4"
#:package Perigon.PostgreSQL@0.1.4
#addin nuget:?package=Perigon.PostgreSQL&version=0.1.4
#tool nuget:?package=Perigon.PostgreSQL&version=0.1.4
Perigon.PostgreSQL
Perigon.PostgreSQL 是一个面向 PostgreSQL 的 .NET 数据访问库,提供类似 EF Core 的 DbContext / DbSet<T> 使用方式。
安装
dotnet add package Perigon.PostgreSQL
定义实体
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("users", Schema = "app")]
public sealed class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("id")]
public int Id { get; set; }
[Column("user_name")]
public string UserName { get; set; } = "";
[Column("tags", TypeName = "text[]")]
public string[] Tags { get; set; } = [];
[Column("profile_json", TypeName = "jsonb")]
public string? ProfileJson { get; set; }
}
定义 DbContext
using Perigon.PostgreSQL;
using Perigon.PostgreSQL.Options;
public sealed class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public AppDbContext(string connectionString)
: base(builder => builder.UseNpgsql(connectionString))
{
}
public DbSet<User> Users => Set<User>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>(entity =>
{
entity.ToTable("users", "app");
entity.Property(x => x.UserName).HasColumnName("user_name").IsRequired();
entity.HasIndex(x => x.UserName)
.HasDatabaseName("uq_users_user_name")
.IsUnique();
});
}
}
创建数据库对象
如果你已经定义好了模型和上下文,可以直接创建 schema / table / foreign key / index:
await using var db = new AppDbContext(connectionString);
await db.EnsureCreatedAsync();
查询与写入
await using var db = new AppDbContext(connectionString);
var users = await db.Users
.Where(x => x.Tags.Contains("postgres"))
.OrderBy(x => x.UserName)
.ToListAsync();
var inserted = await db.Users.InsertAsync(new User
{
UserName = "alice",
Tags = ["developer", "postgres"],
ProfileJson = """{"level":3}"""
});
从现有 PostgreSQL 数据库反向生成代码
安装工具:
dotnet tool install --global dotnet-perigon
最简单的用法:
dotnet perigon database scaffold --connection "Host=localhost;Database=app;Username=postgres;Password=postgres"
默认行为:
--context可省略,默认DefaultDbContext--namespace可省略,默认AppDbContext--output可省略,默认当前目录.DbContext文件默认输出到AppDbContext/<ContextName>.cs- 实体默认输出到
Entity/*.cs - 实体命名空间默认是
AppDbContext.Entity - 默认包含视图;如果不需要,使用
--no-views
常见示例:
dotnet perigon database scaffold `
--connection "Host=localhost;Database=app;Username=postgres;Password=postgres" `
--context MyDbContext `
--namespace MyApp.Data `
--output .\Generated `
--schema public `
--schema audit
可用参数:
--connection必填--context--namespace--output--schema(可重复)--table(可重复)--force--dry-run--no-views
如果你正在这个仓库里开发,也可以直接运行工具项目:
dotnet run --project .\src\Perigon.PostgreSQL.Tools\Perigon.PostgreSQL.Tools.csproj -- database scaffold --connection "Host=localhost;Database=app;Username=postgres;Password=postgres"
ASP.NET Core 注册
using Perigon.PostgreSQL;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("Postgres")!));
示例项目
示例项目位于:
samples/Perigon.PostgreSQL.AspNetCoreSample
启动示例数据库:
cd .\samples\Perigon.PostgreSQL.AspNetCoreSample
docker compose up -d
运行示例:
dotnet run --project .\samples\Perigon.PostgreSQL.AspNetCoreSample\Perigon.PostgreSQL.AspNetCoreSample.csproj --urls http://localhost:5088
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.0)
- Npgsql (>= 10.0.0)
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 |
|---|---|---|
| 0.1.4 | 92 | 5/30/2026 |
| 0.1.3 | 90 | 5/29/2026 |
| 0.1.2 | 92 | 5/28/2026 |
| 0.1.1 | 111 | 5/27/2026 |
| 0.1.0 | 88 | 5/27/2026 |
| 0.1.0-preview2 | 92 | 5/26/2026 |
| 0.1.0-preview1 | 88 | 5/26/2026 |
0.1.4 adds `dotnet perigon database scaffold` with new
defaults/output layout, expands reverse-engineering/runtime parity for
composite keys, default/generated SQL, richer indexes, and views, and
validates the NativeAOT smoke publish path. See RELEASE_NOTES.md for
details.