本库主要是为了满足AOT的需要,并保持 EF Core 的 DbContext / DbSet<T> 使用方式,专门面向小型应用场景的 PostgreSQL ORM,它支持大部分常见的LINQ查询、原生SQL、事务等功能,但不包含变更跟踪,SaveChanges等机制。
dotnet add package Perigon.PostgreSQLusingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;[Table("users",Schema="app")]publicsealedclassUser{[Key][DatabaseGenerated(DatabaseGeneratedOption.Identity)][Column("id")]publicintId{get;set;}[Column("user_name")]publicstringUserName{get;set;}="";[Column("tags",TypeName="text[]")]publicstring[]Tags{get;set;}=[];[Column("profile_json",TypeName="jsonb")]publicstring?ProfileJson{get;set;}}usingPerigon.PostgreSQL;usingPerigon.PostgreSQL.Options;publicsealedclassAppDbContext:DbContext{publicAppDbContext(DbContextOptions<AppDbContext>options):base(options){}publicAppDbContext(stringconnectionString):base(builder =>builder.UseNpgsql(connectionString)){}publicDbSet<User>Users=>Set<User>();protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){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:
awaitusingvardb=newAppDbContext(connectionString);awaitdb.EnsureCreatedAsync();awaitusingvardb=newAppDbContext(connectionString);varusers=awaitdb.Users.Where(x =>x.Tags.Contains("postgres")).OrderBy(x =>x.UserName).ToListAsync();varinserted=awaitdb.Users.InsertAsync(newUser{UserName="alice",Tags=["developer","postgres"],ProfileJson="""{"level":3}"""});安装工具:
dotnet tool install --global Perigon.PostgreSQL.Tools最简单的用法:
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"usingPerigon.PostgreSQL;varbuilder=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