From dc9cf259eb840239f922a0fdfb7b5002bd69280f Mon Sep 17 00:00:00 2001 From: wengxindong <2092619391@qq.com> Date: Mon, 6 Jan 2025 08:51:07 +0800 Subject: [PATCH] 2025 1 3 tijiao --- ...36\346\216\245\347\254\224\350\256\260.md" | 17 +++ ...42\346\225\260\346\215\256\345\272\223.md" | 105 ++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 "\347\277\201\344\277\241\346\240\213/20241230\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245\347\254\224\350\256\260.md" create mode 100644 "\347\277\201\344\277\241\346\240\213/20250103\347\254\224\350\256\260-\346\233\264\346\215\242\346\225\260\346\215\256\345\272\223.md" diff --git "a/\347\277\201\344\277\241\346\240\213/20241230\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245\347\254\224\350\256\260.md" "b/\347\277\201\344\277\241\346\240\213/20241230\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245\347\254\224\350\256\260.md" new file mode 100644 index 0000000..3e1437d --- /dev/null +++ "b/\347\277\201\344\277\241\346\240\213/20241230\346\225\260\346\215\256\345\272\223\350\277\236\346\216\245\347\254\224\350\256\260.md" @@ -0,0 +1,17 @@ +# 数据库连接配置 +```js +using Microsoft.EntityFrameworkCore; +namespace Blog.Models; +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; } = null!; + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + var conString=$"server=.\\SQLEXPRESS;database=Blogs;uid=sa;pwd=123456;TrustServerCertificate=true;"; + optionsBuilder.UseSqlServer(conString); + } + +} +``` \ No newline at end of file diff --git "a/\347\277\201\344\277\241\346\240\213/20250103\347\254\224\350\256\260-\346\233\264\346\215\242\346\225\260\346\215\256\345\272\223.md" "b/\347\277\201\344\277\241\346\240\213/20250103\347\254\224\350\256\260-\346\233\264\346\215\242\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..702a19d --- /dev/null +++ "b/\347\277\201\344\277\241\346\240\213/20250103\347\254\224\350\256\260-\346\233\264\346\215\242\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,105 @@ +# 在Debian上安装PostgreSQL数据库 +## 安装PostgreSQL +在Debian系统上安装PostgreSQL数据库,可以通过以下命令来完成: +``` +sudo apt update +sudo apt install postgresql postgresql-contrib +``` + +## 确认安装状态 +安装完成后,可以通过以下命令来检查PostgreSQL服务的状态: +``` +sudo systemctl status postgresql +``` + +如果服务正在运行,说明安装成功。 + +## 设置高强度密码 +为了提高安全性,需要为PostgreSQL的默认用户 `postgres`设置一个高强度的密码。可以通过以下命令来设置: +``` +sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'your-strong-password';" +``` + +将 `your-strong-password`替换为你选择的密码。 + + +## 设置允许远程访问数据库 +为了允许远程访问PostgreSQL数据库,需要修改配置文件 `postgresql.conf`和 `pg_hba.conf`。 + +### 修改 `postgresql.conf` +打开 `postgresql.conf`文件: +``` +sudo nano /etc/postgresql/12/main/postgresql.conf +``` + +找到 `listen_addresses`行,并将其修改为: +``` +listen_addresses = '*' +``` + +### 修改 `pg_hba.conf` +打开 `pg_hba.conf`文件: +``` +sudo nano /etc/postgresql/12/main/pg_hba.conf +``` + + +在文件的末尾添加以下行,以允许所有IP地址访问数据库: +``` +host all all 0.0.0.0/0 md5 +``` + +保存并关闭文件后,重启PostgreSQL服务: +``` +sudo systemctl restart postgresql +``` + + +## 更换数据库驱动并更新配置 +在你的.NET项目中,需要更换数据库驱动并更新数据库上下文的配置。 + +### 移除原来的数据库驱动 +移除原来的数据库驱动: +``` +dotnet remove package Microsoft.EntityFrameworkCore.SqlServer +``` + +### 安装新的数据库驱动 +安装新的数据库驱动: +``` +dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL +``` + +### 更新数据库上下文配置 +打开你的项目中的 `appsettings.json`或相应的配置文件,将数据库连接字符串更改为PostgreSQL的格式: +``` +{ + "ConnectionStrings": { + "DefaultConnection": "Host=your-server-ip;Port=5432;Database=your-database-name;Username=your-username;Password=your-password" + } +} +``` + +将 `your-server-ip`、`your-database-name`、`your-username`和 `your-password`替换为实际的值。 + + +## 重新生成迁移文件 +在更换数据库驱动并更新配置后,需要重新生成迁移文件以确保迁移与新的数据库兼容。 + +### 删除现有的迁移文件 +删除现有的迁移文件: +``` +dotnet ef migrations remove +``` + +### 添加新的迁移 +添加新的迁移: +``` +dotnet ef migrations add InitialCreate +``` + +### 应用迁移 +最后,应用迁移以创建数据库结构: +``` +dotnet ef database update +``` \ No newline at end of file -- Gitee