From e1de22418c4b0b32e5cd035f7a4b5b915e1d2306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=81=AD=E7=84=95?= <“798441668@qq.com”> Date: Sun, 22 Dec 2024 21:10:13 +0800 Subject: [PATCH] biji --- .../12.15\347\254\224\350\256\260.md" | 58 +++++++++++++++++++ .../12.19\347\254\224\350\256\260.md" | 58 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 "\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.15\347\254\224\350\256\260.md" create mode 100644 "\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.19\347\254\224\350\256\260.md" diff --git "a/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.15\347\254\224\350\256\260.md" "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.15\347\254\224\350\256\260.md" new file mode 100644 index 0000000..856c59b --- /dev/null +++ "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.15\347\254\224\350\256\260.md" @@ -0,0 +1,58 @@ +# 应用EntityFrameworkCode的步骤(可将数据传输到数据库保存) +## 1.安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +## 2.定义数据库表模型 +- 在Models中的Blogs.cs中 +```c# +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. 定义数据库上下文 +- 在Models中创建 BlogDbContext.cs文件 +```c# +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=SK-20240829DIVS\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +``` + +## 4. 生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +``` +dotnet tool install -- global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef migration add InitialCreate +``` + +- 到这里要先在文件Blog.csproj中出现 +```c# + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + +``` +## 5.将上一步生成的迁移文件,更新到数据库:dotnet ef database update +`dotnet ef database update` +(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) + +- 结束后,在文件夹中会出现Migrations文件,就可以在数据库中看到你保存的数据 \ No newline at end of file diff --git "a/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.19\347\254\224\350\256\260.md" "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.19\347\254\224\350\256\260.md" new file mode 100644 index 0000000..da222e2 --- /dev/null +++ "b/\351\231\210\346\201\255\347\204\225/\347\254\224\350\256\260/12.19\347\254\224\350\256\260.md" @@ -0,0 +1,58 @@ +# 可将数据传输到数据库保存 +## 1.安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer + +## 2.定义数据库表模型 +- 在Models中的Blogs.cs中 +```c# +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. 定义数据库上下文 +- 在Models中创建 BlogDbContext.cs文件 +```c# +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=SK-20240829DIVS\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +``` + +## 4. 生成迁移文件,命令:dotnet ef migrations add XXX (PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +``` +dotnet tool install -- global dotnet-ef +dotnet add package Microsoft.EntityFrameworkCore.Design +dotnet ef migration add InitialCreate +``` + +- 到这里要先在文件Blog.csproj中出现 +```c# + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + +``` +## 5.将上一步生成的迁移文件,更新到数据库:dotnet ef database update +`dotnet ef database update` +(PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) + +- 结束后,在文件夹中会出现Migrations文件,就可以在数据库中看到你保存的数据 \ No newline at end of file -- Gitee