From 6c509da3dabef44fa0c34f97139e6c533f56a29a Mon Sep 17 00:00:00 2001 From: huangzhonglei <2195159431@qq.com> Date: Thu, 2 Jun 2022 22:05:39 +0800 Subject: [PATCH 1/4] 1 --- ...72\345\272\223\345\273\272\350\241\250.md" | 4 +- ...73\345\212\240\346\225\210\346\236\234.md" | 117 ++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 "\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" diff --git "a/\351\273\204\345\277\240\347\243\212/2022-05-31 webap\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\345\217\212\345\273\272\345\272\223\345\273\272\350\241\250.md" "b/\351\273\204\345\277\240\347\243\212/2022-05-31 webap\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\345\217\212\345\273\272\345\272\223\345\273\272\350\241\250.md" index b439d69..ebe368b 100644 --- "a/\351\273\204\345\277\240\347\243\212/2022-05-31 webap\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\345\217\212\345\273\272\345\272\223\345\273\272\350\241\250.md" +++ "b/\351\273\204\345\277\240\347\243\212/2022-05-31 webap\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\345\217\212\345\273\272\345\272\223\345\273\272\350\241\250.md" @@ -63,7 +63,7 @@ dotnet add package Microsoft.EntityFrameworkcore.Design -v 3.1 从 3.0 起,EF Core 命令列工具 (dotnet ef) 不在 .NET Core SDK 里面,需另装。命令如下: dotnet tool install --global dotnet-ef 然后再执行 -dotnet ef migrations add InitialCreate -s ../xxx/ +dotnet ef migrations add InitialCreate -s ../webapi/ 再更新数据库 -dotnet ef database update -s ../xxx/ +dotnet ef database update -s ../webapi/ ``` \ No newline at end of file diff --git "a/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" "b/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" new file mode 100644 index 0000000..6fd323b --- /dev/null +++ "b/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" @@ -0,0 +1,117 @@ +## 1.在实例类webapi.domain建Repository文件用于webapi.basics引用 +``` +using System; +using System.Linq;//泛型引用 + +namespace webapi.domain.Repository +{ + public interface IRepository + { + T Getbyid(Guid id); + + IQueryable Table { get; }//此接口继承接口 IEnumerable + + void Insert(T entity); + + void delete(Guid id); + + void update(T entity); + } +} + +``` +## 2.webapi.basics中的Repository.cs文件 +``` +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() + { + _Db = new config(); + _table = _Db.Set(); + } + public IQueryable Table + { + get + { + return _table.AsNoTracking(); + } + } + public T Getbyid(Guid id) + { + var entity = + _Db + .Set() + .AsQueryable() + .Where(x => x.id == id) + .FirstOrDefault(); + return entity; + } + public void Insert(T entity) + { + entity.status = true; + entity.CreateTime = DateTime.Now; + entity.updateTime = DateTime.Now; + _table.Add (entity); + _Db.SaveChanges(); + } + public void update(T entity) + { + } + public void delete(Guid Id) + { + } + } +} +数据迁移 +dotnet ef migrations add InitialCreate -s ../webapi/ +更新数据库 +dotnet ef database update -s ../webapi/ +``` +## 3.在webapi中Controllers建UserControllers.cs +``` +using System; +using System.Linq; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc; +using Soft1ApiDemo.Domain.Repository; +using Soft1ApiDemo.Domain.Entity; + +namespace Soft1ApiDemo.Api +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase + { + private readonly IRepository _userRes; + public UsersController(IRepository userRes) + { + _userRes = userRes; + } + [Route("test")] + public IEnumerable Index() + { + //在Users表中添加数据 + _userRes.Add(new Users{ + Username="8888", + Password="9999" + }); + var id=Guid.NewGuid(); + var xx=_userRes.Table.Where(x=>x.Username=="").ToArray(); + return xx; + } + } +} + +``` \ No newline at end of file -- Gitee From b932126f9f579731027a8a62aa9d5428e4d89174 Mon Sep 17 00:00:00 2001 From: huangzhonglei <2195159431@qq.com> Date: Thu, 2 Jun 2022 22:33:02 +0800 Subject: [PATCH 2/4] 1 --- ...73\345\212\240\346\225\210\346\236\234.md" | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git "a/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" "b/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" index 6fd323b..574473d 100644 --- "a/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" +++ "b/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" @@ -75,43 +75,43 @@ namespace webapi.basics.Repository } } 数据迁移 -dotnet ef migrations add InitialCreate -s ../webapi/ +dotnet ef migrations add InitialCreate -s ../webapis/ 更新数据库 -dotnet ef database update -s ../webapi/ +dotnet ef database update -s ../webapis/ ``` -## 3.在webapi中Controllers建UserControllers.cs +## 3.在webapis中Controllers建UserControllers.cs ``` using System; -using System.Linq; using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; -using Soft1ApiDemo.Domain.Repository; -using Soft1ApiDemo.Domain.Entity; - -namespace Soft1ApiDemo.Api +using Microsoft.Extensions.Logging; +using webapi.domain.Entity; +using webapi.domain.Repository; +namespace webapis { - [ApiController] + [ApiController] [Route("[controller]")] - public class UsersController : ControllerBase + + public class UsersControllers:ControllerBase { - private readonly IRepository _userRes; - public UsersController(IRepository userRes) - { - _userRes = userRes; + private readonly IRepository _user; + public UsersControllers(IRepository user){ + _user=user; } - [Route("test")] - public IEnumerable Index() - { - //在Users表中添加数据 - _userRes.Add(new Users{ - Username="8888", - Password="9999" + [Route("/text")] + public IEnumerable Index(){ + _user.Add(new Users{ + UsersName="猪猪", + Age="110" }); var id=Guid.NewGuid(); - var xx=_userRes.Table.Where(x=>x.Username=="").ToArray(); + var xx=_user.Table.Where(x=>x.UsersName=="").ToArray(); return xx; } + } + } - ``` \ No newline at end of file -- Gitee From b52b3535333fc62217a858dc0a5a205976bf1b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BF=A0=E7=A3=8A?= <2195159431@qq.com> Date: Fri, 3 Jun 2022 00:11:07 +0000 Subject: [PATCH 3/4] =?UTF-8?q?update=20=E9=BB=84=E5=BF=A0=E7=A3=8A/2022-0?= =?UTF-8?q?6-02=E5=B0=81=E8=A3=85=E5=A2=9E=E5=88=A0=E6=94=B9=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=9E=E7=8E=B0=E6=95=B0=E6=8D=AE=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=95=88=E6=9E=9C.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\256\346\267\273\345\212\240\346\225\210\346\236\234.md" | 5 +++++ 1 file changed, 5 insertions(+) diff --git "a/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" "b/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" index 574473d..66ccd48 100644 --- "a/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" +++ "b/\351\273\204\345\277\240\347\243\212/2022-06-02\345\260\201\350\243\205\345\242\236\345\210\240\346\224\271\346\216\245\345\217\243\345\256\236\347\216\260\346\225\260\346\215\256\346\267\273\345\212\240\346\225\210\346\236\234.md" @@ -114,4 +114,9 @@ namespace webapis } } +``` +## 注意在Startup.cs文件中 +``` + // 依赖注入 将CRUD的实现类注册到接口 + services.AddScoped(typeof(IRepository<>),typeof(RepositoryBase<>)); ``` \ No newline at end of file -- Gitee From 5b551b4074c1d0e091293677cad301509a6fff55 Mon Sep 17 00:00:00 2001 From: huangzhonglei <2195159431@qq.com> Date: Fri, 3 Jun 2022 22:17:54 +0800 Subject: [PATCH 4/4] 1 --- ...36\345\210\240\346\224\271\346\237\245.md" | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 "\351\273\204\345\277\240\347\243\212/2022-06-03\345\256\236\347\216\260\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\344\270\212\344\270\213\346\226\207\344\276\235\350\265\226\346\263\250\345\206\214\345\217\212\347\256\200\345\215\225\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" diff --git "a/\351\273\204\345\277\240\347\243\212/2022-06-03\345\256\236\347\216\260\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\344\270\212\344\270\213\346\226\207\344\276\235\350\265\226\346\263\250\345\206\214\345\217\212\347\256\200\345\215\225\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\351\273\204\345\277\240\347\243\212/2022-06-03\345\256\236\347\216\260\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\344\270\212\344\270\213\346\226\207\344\276\235\350\265\226\346\263\250\345\206\214\345\217\212\347\256\200\345\215\225\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000..ba93bf0 --- /dev/null +++ "b/\351\273\204\345\277\240\347\243\212/2022-06-03\345\256\236\347\216\260\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223\344\270\212\344\270\213\346\226\207\344\276\235\350\265\226\346\263\250\345\206\214\345\217\212\347\256\200\345\215\225\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,163 @@ +## 连接数据库上下文依赖注册 +### 1.在appsettings.json中定义字段 +``` +"ConnectionStrings": { + "SqlServer":"server=.;database=Soft1ApiDemo;uid=sa;pwd=123456;" + } +``` +### 2.在Startup.cs中添加以下代码 +``` + + var _conString=Configuration.GetConnectionString("SqlServer"); + services.AddDbContext(options=>options.UseSqlServer(_conString)); + +``` +### 3.在webapi.bacis.Db文件中(连接数据库)添加 +``` + public config(DbContextOptions options):base(options){ + + } +``` +## 实现增删改查 +### 在ERepository.cs相当与后端代码 +``` +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文件中 +### 查找 +``` + private readonly IRepository _user;//定义只读的泛型表 + + public UsersController(IRepository user) + { + _user = user; + } +显示首页 + public IEnumerable Index() + { + var list = _user.Table.ToList(); + return list; + } +``` +### 添加 +``` + [HttpPost] + public Users Create() + { + var user = new Users { UsersName = "人才", Age = "888" }; + var list = _user.Add(user); + return list; + } + +``` +### 更新 +``` + [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; } + } +``` +### 删除 +``` + [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