# SqlSugarPgDemo **Repository Path**: Peter_Mond/sql-sugar-pg-demo ## Basic Information - **Project Name**: SqlSugarPgDemo - **Description**: asp.net core中SqlSugarCore基础使用 - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-06-17 - **Last Updated**: 2026-06-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SqlSugarPgDemo ## 介绍 基于 **.NET 8** + **ASP.NET Core Web API** + **SqlSugarCore ORM** + **PostgreSQL** 的用户 CRUD 示例项目,演示了如何在 asp.net core 中使用 SqlSugarCore 进行基础开发。 ## 软件架构 ``` sql-sugar-pg-demo/ ├── SqlSugarPgDemo.slnx └── src/SqlSugarPgDemo/ ├── Program.cs # 应用入口 & 服务注册(SqlSugar / Swagger) ├── SqlSugarPgDemo.csproj # 项目依赖:SqlSugarCore 5.1.4.214, Swashbuckle 6.6.2 ├── appsettings.json # 生产环境配置(PostgreSQL 连接字符串) ├── appsettings.Development.json # 开发环境配置 ├── SqlSugarPgDemo.http # HTTP 请求测试文件 ├── Properties/launchSettings.json# 启动配置 (http://localhost:5045) ├── Controllers/ │ └── UserController.cs # 用户 CRUD API ├── Entity/ │ └── User.cs # 用户实体(映射 sys_user 表) └── Extensions/ └── Extensions.cs # 数据库表初始化扩展方法 ``` **核心技术栈:** - **.NET 8.0** - **ASP.NET Core Web API** - **SqlSugarCore 5.1.4.214** — ORM 框架(CodeFirst、AOP 日志) - **PostgreSQL** — 关系型数据库 - **Swagger (Swashbuckle.AspNetCore 6.6.2)** — API 文档 ## 安装教程 ### 1. 环境要求 - .NET 8.0 SDK - PostgreSQL 数据库服务(本示例默认端口 `54322`) ### 2. 数据库配置 修改 `src/SqlSugarPgDemo/appsettings.json` 中的数据库连接信息: ```json { "ConnectionStrings": { "PgSqlConnection": "Host=localhost;Port=54322;Database=SqlSugarPgDb;Username=admin;Password=123456;" } } ``` ### 3. 恢复包并运行 ```powershell dotnet restore dotnet run --project src/SqlSugarPgDemo ``` 首次运行会自动通过 CodeFirst 创建 `sys_user` 表。 ## 使用说明 ### API 列表(基础路由:`/api/User`) | HTTP 方法 | 路径 | 描述 | |-----------|------|------| | POST | `/api/User/Add` | 新增用户(Body: User),返回自增 ID | | GET | `/api/User/GetById/{id}` | 按 ID 查询单个用户 | | GET | `/api/User/GetAll` | 查询所有用户 | | GET | `/api/User/GetPage?pageIndex=1&pageSize=10` | 分页查询 | | PUT | `/api/User/Update` | 修改用户名和手机号(Body: User) | | DELETE | `/api/User/Delete/{id}` | 按 ID 删除用户 | ### 启动后访问 - **Swagger 文档**:http://localhost:5045/swagger - **控制台**:每次数据库操作都会打印执行的 SQL 语句(通过 SqlSugar 的 `OnLogExecuting` AOP 拦截器) ### User 实体字段 | 字段 | 类型 | 约束 | |------|------|------| | Id | int | 主键,自增 | | UserName | string (50) | 非空 | | Phone | string (11) | 可空 | | CreateTime | DateTime? | 可空 | ## 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request