From 0241bd24c4c85829fe42f59e49c5fd66a1a951fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E5=B9=BF=E7=94=9F?= <1307421235@qq.com> Date: Wed, 1 Jun 2022 02:48:51 +0000 Subject: [PATCH 1/5] =?UTF-8?q?add=20=E6=B8=A9=E5=B9=BF=E7=94=9F/20200601w?= =?UTF-8?q?eib.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20200601weib.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "\346\270\251\345\271\277\347\224\237/20200601weib.md" diff --git "a/\346\270\251\345\271\277\347\224\237/20200601weib.md" "b/\346\270\251\345\271\277\347\224\237/20200601weib.md" new file mode 100644 index 0000000..a4979f1 --- /dev/null +++ "b/\346\270\251\345\271\277\347\224\237/20200601weib.md" @@ -0,0 +1,26 @@ +dotnet new classlib -o Soft1ApiDemo.Infrastructure + +cd .\Soft1ApiDemo.Infrastructure\ + +dotnet add package Microsoft.EntityFrameworkcore -v 3.1 + + + + + +dotnet restore + + cd ..\Soft1ApiDemo.Api\ + +dotnet add package Microsoft.EntityFrameworkCore.Design -v 3.1 + + +dotnet add .\Soft1ApiDemo.Api\ reference .\Soft1ApiDemo.Infrastructure\ + +cd .\Soft1ApiDemo.Infrastructure\ + +dotnet ef migrations add 888 -s ..\Soft1ApiDemo.Api + +dotnet ef database update -s ..\Soft1ApiDemo.Api\ + + -- Gitee From ad4cfbe13d62086678daca42a936213b8e075edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E5=B9=BF=E7=94=9F?= <1307421235@qq.com> Date: Wed, 1 Jun 2022 02:50:49 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20=E6=B8=A9?= =?UTF-8?q?=E5=B9=BF=E7=94=9F/20200601weib.md=20=E4=B8=BA=20=E6=B8=A9?= =?UTF-8?q?=E5=B9=BF=E7=94=9F/20220601-webapi.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20220601-webapi.md" | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename "\346\270\251\345\271\277\347\224\237/20200601weib.md" => "\346\270\251\345\271\277\347\224\237/20220601-webapi.md" (100%) diff --git "a/\346\270\251\345\271\277\347\224\237/20200601weib.md" "b/\346\270\251\345\271\277\347\224\237/20220601-webapi.md" similarity index 100% rename from "\346\270\251\345\271\277\347\224\237/20200601weib.md" rename to "\346\270\251\345\271\277\347\224\237/20220601-webapi.md" -- Gitee From 4b76e3d4faffeb29315ac7e20120546d13d55466 Mon Sep 17 00:00:00 2001 From: xiao-a-qi <1307421235@qq.com> Date: Wed, 1 Jun 2022 15:13:00 +0800 Subject: [PATCH 3/5] 0 --- .../20220601-webapi.md" | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git "a/\346\270\251\345\271\277\347\224\237/20220601-webapi.md" "b/\346\270\251\345\271\277\347\224\237/20220601-webapi.md" index a4979f1..fd500e8 100644 --- "a/\346\270\251\345\271\277\347\224\237/20220601-webapi.md" +++ "b/\346\270\251\345\271\277\347\224\237/20220601-webapi.md" @@ -1,26 +1,46 @@ +## 创建Soft1ApiDemo.Infrastructure +```js dotnet new classlib -o Soft1ApiDemo.Infrastructure +``` +## 进入Infrastructure +```js cd .\Soft1ApiDemo.Infrastructure\ +``` +## 添加3.1版本 +```js dotnet add package Microsoft.EntityFrameworkcore -v 3.1 - +``` +## 重启 +``` dotnet restore - +``` +## 进入 +``` cd ..\Soft1ApiDemo.Api\ - +``` +## 创建Design3.1版本 +```js dotnet add package Microsoft.EntityFrameworkCore.Design -v 3.1 - - +``` +## 添加在infrastructure理 +```js dotnet add .\Soft1ApiDemo.Api\ reference .\Soft1ApiDemo.Infrastructure\ - +``` +## 进入infrast +```js cd .\Soft1ApiDemo.Infrastructure\ - +``` +## 同步数据库 +```js dotnet ef migrations add 888 -s ..\Soft1ApiDemo.Api - +``` +## 更新数据库 +```js dotnet ef database update -s ..\Soft1ApiDemo.Api\ - - +``` -- Gitee From 77638b255c2b15c526080de5750200ae9bcaa4f4 Mon Sep 17 00:00:00 2001 From: xiao-a-qi <1307421235@qq.com> Date: Thu, 2 Jun 2022 21:46:03 +0800 Subject: [PATCH 4/5] 0 --- .../20220602-webapi.md" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "\346\270\251\345\271\277\347\224\237/20220602-webapi.md" diff --git "a/\346\270\251\345\271\277\347\224\237/20220602-webapi.md" "b/\346\270\251\345\271\277\347\224\237/20220602-webapi.md" new file mode 100644 index 0000000..5b1fdab --- /dev/null +++ "b/\346\270\251\345\271\277\347\224\237/20220602-webapi.md" @@ -0,0 +1,33 @@ +首先在多项目中 一个项目需要引用另外一个项目,就需要将他引入进配置 + +~~~ + +dotnet add reference ../项目地址 +~~~ + +在引用之后,就可以进行using 使用到另一个项目的代码和类等等 + +## 基础的安装 EFcore 连接数据库 + +~~~ +dotnet add package Microsof entity framework core -v 指定版本号 + +dotnet add package Microsoft.EntityFrameWork.SqlServer -v "版本号" + +dotnet add package Microsoft.EntityFrameWork.Design -v "版本号" + +~~~ + +安装这个之后,还需要装一下ef的迁移工具 + +~~~ +dotnet tool install --global dotnet-ef + +~~~ +当前面的步骤都走完了之后呢,就可以执行命令了 +~~~ +dotnet ef migrations add 文件名 +dotnet ef datebase update + +~~~ +这样就更新到了成功了 \ No newline at end of file -- Gitee From 83c9e231f51fbe96b14f5d32dd40d36dc251b43e Mon Sep 17 00:00:00 2001 From: xiao-a-qi <1307421235@qq.com> Date: Mon, 6 Jun 2022 12:04:20 +0800 Subject: [PATCH 5/5] 0 --- .../20220602-webapi.md" | 1 + .../20220603-webapi.md" | 162 ++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 "\346\270\251\345\271\277\347\224\237/20220603-webapi.md" diff --git "a/\346\270\251\345\271\277\347\224\237/20220602-webapi.md" "b/\346\270\251\345\271\277\347\224\237/20220602-webapi.md" index 5b1fdab..ae410fb 100644 --- "a/\346\270\251\345\271\277\347\224\237/20220602-webapi.md" +++ "b/\346\270\251\345\271\277\347\224\237/20220602-webapi.md" @@ -16,6 +16,7 @@ dotnet add package Microsoft.EntityFrameWork.SqlServer -v "版本号" dotnet add package Microsoft.EntityFrameWork.Design -v "版本号" + ~~~ 安装这个之后,还需要装一下ef的迁移工具 diff --git "a/\346\270\251\345\271\277\347\224\237/20220603-webapi.md" "b/\346\270\251\345\271\277\347\224\237/20220603-webapi.md" new file mode 100644 index 0000000..ae360c6 --- /dev/null +++ "b/\346\270\251\345\271\277\347\224\237/20220603-webapi.md" @@ -0,0 +1,162 @@ +# 连接数据库上下文依赖注册 + +## 1.在appsettings.json中定义字段 +```js +"ConnectionStrings": { + "SqlServer":"server=.;database=Soft1ApiDemo;uid=sa;pwd=123456;" + } +``` + +## 2.在Startup.cs中添加以下代码 +```js + var _conString=Configuration.GetConnectionString("SqlServer"); + services.AddDbContext(options=>options.UseSqlServer(_conString)); +``` + +# 实现增删改查 + +## 在ERepository.cs相当与后端代码 +```js +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using webapi.basics.Db; +using webapi.domain; +using webapi.domain.Repository; +namespace webapi.basics.Repository +{ + public class ERepository : IRepository where T : BaseEntity + { + private readonly config _Db;//定义数据库 + private readonly DbSet _table;//定义表 + public ERepository(config Db) + { + _Db = Db; + _table = _Db.Set(); + } + IQueryable 接口旨在供查询 + public IQueryable Table + { + get + { + return _table.AsNoTracking(); + } + } +获取id + public T Getbyid(Guid id) + { + var entity = + _Db + .Set() + .AsQueryable() + .Where(x => x.id == id) + .FirstOrDefault(); + return entity; + } +添加 + public T Add(T entity) + { + entity.status = true; + entity.CreateTime = DateTime.Now; + entity.UpdateTime = DateTime.Now; + _table.Add (entity); + _Db.SaveChanges(); + return entity; + } + 更新 + public void update(T entity) + { + entity.UpdateTime = DateTime.Now; + _table.Update (entity); + _Db.SaveChanges(); + } +删除 + public void delete(Guid Id) + { + var tem = this.Getbyid(Id); + if (tem != null) + { + _table.Remove (tem); + _Db.SaveChanges(); + } + } +伪删除 + // } + // public void delete(Guid Id) + // { + // var tem=this.Getbyid(Id); + // if (tem!=null) + // { + //entity.UpdateTime=DateTime.Now; + // _table.Update(entity); + // _Db.SaveChanges(); + + // } + // } + } +} +``` + +# Controllers文件中 +## 查找 +```js + private readonly IRepository _user;//定义只读的泛型表 + + public UsersController(IRepository user) + { + _user = user; + } +显示首页 + public IEnumerable Index() + { + var list = _user.Table.ToList(); + return list; + } +``` + +## 添加 +```js + [HttpPost] + public Users Create() + { + var user = new Users { UsersName = "人才", Age = "888" }; + var list = _user.Add(user); + return list; + } +``` + +## 更新 +```js + [HttpPut] + [Route("{id}")] + public Users updates(Guid id, datas model) + { + var user = _user.Getbyid(id); + if (user != null) + { + user.UsersName = model.UsersName; + user.Age = model.Age; + _user.update (user); + } + return user; + } + 定义实体类 + public class datas + { + public string UsersName { get; set; } + + public string Age { get; set; } + } +``` + +## 删除 +```js + [HttpDelete] + [Route("{id}")] + public dynamic deletes(Guid id) + { + System.Console.WriteLine (id); + _user.delete (id); + return new { code = 1000, Msg = "删除成功", data = "" }; + } +``` \ No newline at end of file -- Gitee