From a15303511da81bcdc96af870f274f2a855673bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=86=E6=BB=A2=E9=92=B0?= <14091774+well-yes@user.noreply.gitee.com> Date: Sun, 15 Dec 2024 18:44:30 +0800 Subject: [PATCH] 111 --- .../20241209.md" | 35 +++++++++++++++++++ .../20241212.md" | 1 + 2 files changed, 36 insertions(+) create mode 100644 "\351\231\206\346\273\242\351\222\260/20241209.md" create mode 100644 "\351\231\206\346\273\242\351\222\260/20241212.md" diff --git "a/\351\231\206\346\273\242\351\222\260/20241209.md" "b/\351\231\206\346\273\242\351\222\260/20241209.md" new file mode 100644 index 0000000..04fceae --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241209.md" @@ -0,0 +1,35 @@ +## 用EntityFrameworkCore的步骤 +1. 安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer +2. 定义数据库表模型 +```csharp +namespace Blog.Models; + +public class Blogs +{ + public int Id { get; set; } + public string Title { get; set; } = null!; + public string Content { get; set; } = null!; + public string Author { get; set; } = null!; +} +``` + +3. 定义数据库上下文 +```csharp +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; } = null!; + + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=.\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +``` +4. 生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +5. 将上一步生成的迁移文件,更新到数据库:dotnet ef database update(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) \ No newline at end of file diff --git "a/\351\231\206\346\273\242\351\222\260/20241212.md" "b/\351\231\206\346\273\242\351\222\260/20241212.md" new file mode 100644 index 0000000..a276245 --- /dev/null +++ "b/\351\231\206\346\273\242\351\222\260/20241212.md" @@ -0,0 +1 @@ +今天讲了练习 \ No newline at end of file -- Gitee