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/2] 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/2] 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