From 0ba58782de1aae61222b761cc92f7208943585b2 Mon Sep 17 00:00:00 2001 From: zou-gode <1256960051@qq.com> Date: Thu, 2 Jun 2022 17:38:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?CRUD=E7=9A=84=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/launch.json" | 36 +++++++++++++ .../.vscode/tasks.json" | 42 +++++++++++++++ .../lianxi/api.http" | 6 +++ ...3\230\350\256\244\345\200\274.Designer.cs" | 4 +- ...13\351\273\230\350\256\244\345\200\274.cs" | 2 +- .../Repository/EFRepository.cs" | 51 +++++++++++++++++++ .../Controllers/UsersController.cs" | 41 +++++++++++++++ .../lianxi/webapi.domain/BaseEntity.cs" | 8 +-- .../webapi.domain/Repository/IRepository.cs" | 18 +++++++ 9 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 "\351\202\271\346\265\267\345\205\265/.vscode/launch.json" create mode 100644 "\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" create mode 100644 "\351\202\271\346\265\267\345\205\265/lianxi/api.http" rename "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.Designer.cs" => "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.Designer.cs" (98%) rename "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.cs" => "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.cs" (98%) create mode 100644 "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" create mode 100644 "\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" create mode 100644 "\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" diff --git "a/\351\202\271\346\265\267\345\205\265/.vscode/launch.json" "b/\351\202\271\346\265\267\345\205\265/.vscode/launch.json" new file mode 100644 index 0000000..05aa41a --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/.vscode/launch.json" @@ -0,0 +1,36 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/lianxi/webapi.api/bin/Debug/netcoreapp3.1/webapi.api.dll", + "args": [], + "cwd": "${workspaceFolder}/lianxi/webapi.api", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" "b/\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" new file mode 100644 index 0000000..7c46bb0 --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" @@ -0,0 +1,42 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/lianxi/webapi.api/webapi.api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/lianxi/webapi.api/webapi.api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "${workspaceFolder}/lianxi/webapi.api/webapi.api.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/api.http" "b/\351\202\271\346\265\267\345\205\265/lianxi/api.http" new file mode 100644 index 0000000..babdae4 --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/api.http" @@ -0,0 +1,6 @@ +@url=http://localhost:5000 + +### +GET {{url}}/Users/test HTTP/1.1 +### +GET {{url}}/Users/add HTTP/1.1 \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.Designer.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.Designer.cs" similarity index 98% rename from "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.Designer.cs" rename to "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.Designer.cs" index 45537eb..758b708 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.Designer.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.Designer.cs" @@ -10,8 +10,8 @@ using webapi.Infrastructure.Db; namespace webapi.Infrastructure.Migrations { [DbContext(typeof(webapiDbContext))] - [Migration("20220531022514_1")] - partial class _1 + [Migration("20220602085932_模型默认值")] + partial class 模型默认值 { protected override void BuildTargetModel(ModelBuilder modelBuilder) { diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.cs" similarity index 98% rename from "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.cs" rename to "\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.cs" index 83ad2fb..6088660 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220531022514_1.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Migrations/20220602085932_\346\250\241\345\236\213\351\273\230\350\256\244\345\200\274.cs" @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace webapi.Infrastructure.Migrations { - public partial class _1 : Migration + public partial class 模型默认值 : Migration { protected override void Up(MigrationBuilder migrationBuilder) { diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" new file mode 100644 index 0000000..be82acd --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" @@ -0,0 +1,51 @@ +using System; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using webapi.Domain; +using webapi.Domain.Repository; +using webapi.Infrastructure.Db; + +namespace webapi.Infrastructure.Repository +{ + public class EFRepository : IRepository where T : BaseEntity + { + private readonly webapiDbContext _db; + + private readonly DbSet _table; + public IQueryable Table + { + get + { + return _table as IQueryable; + } + } + + public EFRepository() + { + _db = new webapiDbContext(); + _table = _db.Set(); + } + + + + public void Add(T entity) + { + } + + public void Delete(T entity) + { + throw new NotImplementedException(); + } + + public T getById(Guid id) + { + var entity = _table.Where(x=>x.Id == id &&x.IsDeleted==false).FirstOrDefault(); + return entity; + } + + public void Update(T entity) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" new file mode 100644 index 0000000..2234fb8 --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" @@ -0,0 +1,41 @@ +using System; +using webapi.Infrastructure.Repository; +using Microsoft.AspNetCore.Mvc; +using webapi.Infrastructure.Db; +using Webapi.Domain.Entity; +using System.Collections.Generic; +using webapi.Domain.Repository; + +namespace webapi.api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class UsersController : ControllerBase + { + private readonly webapiDbContext _db; + private readonly EFRepository _table; + + public UsersController() + { + _db = new webapiDbContext(); + _table = new EFRepository(); + } + [Route("test")] + public string Index() + { + var id = Guid.NewGuid(); + var users = _table.getById(id); + return "1111"; + } + + [Route("add")] + public void Add() + { + _table.Add(new Users + { + UserName = "张三", + Password = "123" + }); + } + } +} \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" index 8bc3ef7..3785b32 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/BaseEntity.cs" @@ -8,10 +8,10 @@ namespace webapi.Domain public Guid Id { get; set; } // 标识记录是否启用 - public bool IsActived { get; set; } + public bool IsActived { get; set; }=true; // 标识记录是否删除 - public bool IsDeleted { get; set; } + public bool IsDeleted { get; set; }=false; // 创建者的Id public Guid? CreatedBy { get; set; } @@ -20,10 +20,10 @@ namespace webapi.Domain public Guid? UpdatedBy { get; set; } // 创建时间 - public DateTime CreatedAt { get; set; } + public DateTime CreatedAt { get; set; }=DateTime.Now; // (最后)修改时间 - public DateTime UpdatedAt { get; set; } + public DateTime UpdatedAt { get; set; }=DateTime.Now; // 展示的顺序 public int DisplayOrder { get; set; } diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" new file mode 100644 index 0000000..4fca83e --- /dev/null +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" @@ -0,0 +1,18 @@ +using System; +using System.Linq; + +namespace webapi.Domain.Repository +{ + public interface IRepository + { + T getById(Guid id); + + IQueryable Table{get;} + + void Add(T entity); + + void Update(T entity); + + void Delete(T entity); + } +} \ No newline at end of file -- Gitee From f546d3cf4fc59ffed7c1c885e3cb5c3c5986acfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=B5=B7=E5=85=B5?= <1256960051@qq.com> Date: Thu, 2 Jun 2022 09:38:46 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E9=82=B9=E6=B5=B7=E5=85=B5/.vscode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.vscode/launch.json" | 36 ---------------- .../.vscode/tasks.json" | 42 ------------------- 2 files changed, 78 deletions(-) delete mode 100644 "\351\202\271\346\265\267\345\205\265/.vscode/launch.json" delete mode 100644 "\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" diff --git "a/\351\202\271\346\265\267\345\205\265/.vscode/launch.json" "b/\351\202\271\346\265\267\345\205\265/.vscode/launch.json" deleted file mode 100644 index 05aa41a..0000000 --- "a/\351\202\271\346\265\267\345\205\265/.vscode/launch.json" +++ /dev/null @@ -1,36 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - // Use IntelliSense to find out which attributes exist for C# debugging - // Use hover for the description of the existing attributes - // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md - "name": ".NET Core Launch (web)", - "type": "coreclr", - "request": "launch", - "preLaunchTask": "build", - // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/lianxi/webapi.api/bin/Debug/netcoreapp3.1/webapi.api.dll", - "args": [], - "cwd": "${workspaceFolder}/lianxi/webapi.api", - "stopAtEntry": false, - // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser - "serverReadyAction": { - "action": "openExternally", - "pattern": "\\bNow listening on:\\s+(https?://\\S+)" - }, - "env": { - "ASPNETCORE_ENVIRONMENT": "Development" - }, - "sourceFileMap": { - "/Views": "${workspaceFolder}/Views" - } - }, - { - "name": ".NET Core Attach", - "type": "coreclr", - "request": "attach", - "processId": "${command:pickProcess}" - } - ] -} \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" "b/\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" deleted file mode 100644 index 7c46bb0..0000000 --- "a/\351\202\271\346\265\267\345\205\265/.vscode/tasks.json" +++ /dev/null @@ -1,42 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "command": "dotnet", - "type": "process", - "args": [ - "build", - "${workspaceFolder}/lianxi/webapi.api/webapi.api.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "publish", - "command": "dotnet", - "type": "process", - "args": [ - "publish", - "${workspaceFolder}/lianxi/webapi.api/webapi.api.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - }, - { - "label": "watch", - "command": "dotnet", - "type": "process", - "args": [ - "watch", - "run", - "${workspaceFolder}/lianxi/webapi.api/webapi.api.csproj", - "/property:GenerateFullPaths=true", - "/consoleloggerparameters:NoSummary" - ], - "problemMatcher": "$msCompile" - } - ] -} \ No newline at end of file -- Gitee From e0a4210c9af1b5a74b81d241a6c79245713e9a57 Mon Sep 17 00:00:00 2001 From: zou-gode <1256960051@qq.com> Date: Fri, 3 Jun 2022 11:35:08 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=8C=E6=88=90CRUD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lianxi/api.http" | 22 +++++- .../Db/webapiDbContext.cs" | 20 +++-- .../Repository/EFRepository.cs" | 44 +++++++---- .../Controllers/UsersController.cs" | 74 ++++++++++++++----- .../lianxi/webapi.api/Startup.cs" | 9 +++ .../lianxi/webapi.api/appsettings.json" | 5 +- .../webapi.domain/Repository/IRepository.cs" | 2 +- 7 files changed, 134 insertions(+), 42 deletions(-) diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/api.http" "b/\351\202\271\346\265\267\345\205\265/lianxi/api.http" index babdae4..1cbb5b9 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/api.http" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/api.http" @@ -1,6 +1,24 @@ @url=http://localhost:5000 ### -GET {{url}}/Users/test HTTP/1.1 +GET {{url}}/users HTTP/1.1 + +### +POST {{url}}/users HTTP/1.1 +Content-Type: application/json + +{ + "Username":"admin", + "Password":"113" +} + +### +PUT {{url}}/users/6C1FFF06-27DC-45C0-6CF8-08DA450D94C4 HTTP/1.1 +Content-Type: application/json + +{ + "Username":"1231231", + "Password":"sadfas" +} ### -GET {{url}}/Users/add HTTP/1.1 \ No newline at end of file +DELETE {{url}}/users/6C1FFF06-27DC-45C0-6CF8-08DA450D94C4 HTTP/1.1 \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" index a9b5236..6cb8a69 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Db/webapiDbContext.cs" @@ -1,20 +1,24 @@ using Microsoft.EntityFrameworkCore; using Webapi.Domain.Entity; -namespace webapi.Infrastructure.Db +namespace webapi.Infrastructure.Db { - public class webapiDbContext: DbContext + public class webapiDbContext : DbContext { - private readonly string - _conString = "server=.;database=webapi;uid=sa;pwd=123456;"; + private readonly string + _conString = "server=.;database=webapi;uid=sa;pwd=123456;"; + public webapiDbContext(DbContextOptions options) : base(options) + { + + } protected override void OnConfiguring(DbContextOptionsBuilder builder) { - builder.UseSqlServer (_conString); + builder.UseSqlServer(_conString); } - public DbSet Users {get;set;} - public DbSet Roles {get;set;} - public DbSet UserRoles {get;set;} + public DbSet Users { get; set; } + public DbSet Roles { get; set; } + public DbSet UserRoles { get; set; } } } \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" index be82acd..6df1b98 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.Infrastructure/Repository/EFRepository.cs" @@ -14,38 +14,56 @@ namespace webapi.Infrastructure.Repository private readonly DbSet _table; public IQueryable Table { - get + get { - return _table as IQueryable; + return _table.AsNoTracking(); + // return _table as IQueryable; } } - public EFRepository() + public EFRepository(webapiDbContext db) { - _db = new webapiDbContext(); + _db = db; _table = _db.Set(); } - - public void Add(T entity) + public T Add(T entity) { - } + entity.IsActived = true; + entity.IsDeleted = false; + entity.CreatedAt = DateTime.Now; + entity.UpdatedAt = DateTime.Now; + entity.DisplayOrder = 0; - public void Delete(T entity) - { - throw new NotImplementedException(); - } + _table.Add(entity); + _db.SaveChanges(); + return entity; + } public T getById(Guid id) { - var entity = _table.Where(x=>x.Id == id &&x.IsDeleted==false).FirstOrDefault(); + // var entity = _table.Where(x => x.Id == id && x.IsDeleted == false).FirstOrDefault(); + var entity = + _db + .Set() + .AsQueryable() + .Where(x => x.Id == id) + .FirstOrDefault(); return entity; } public void Update(T entity) { - throw new NotImplementedException(); + entity.UpdatedAt = DateTime.Now; + _table.Update(entity); + _db.SaveChanges(); + } + + public void Delete(T entity) + { + _table.Update(entity); + _db.SaveChanges(); } } } \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" index 2234fb8..273c139 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Controllers/UsersController.cs" @@ -5,6 +5,7 @@ using webapi.Infrastructure.Db; using Webapi.Domain.Entity; using System.Collections.Generic; using webapi.Domain.Repository; +using System.Linq; namespace webapi.api.Controllers { @@ -12,30 +13,69 @@ namespace webapi.api.Controllers [Route("[controller]")] public class UsersController : ControllerBase { - private readonly webapiDbContext _db; - private readonly EFRepository _table; + // private readonly webapiDbContext _db; + private readonly IRepository _users; - public UsersController() + public UsersController(IRepository users) { - _db = new webapiDbContext(); - _table = new EFRepository(); + // _db = new webapiDbContext(); + // _users = new EFRepository(); + _users = users; } - [Route("test")] - public string Index() + public IEnumerable Index() { - var id = Guid.NewGuid(); - var users = _table.getById(id); - return "1111"; + var users = _users.Table.ToArray(); + return users; } - [Route("add")] - public void Add() + [HttpPost] + public Users CreateUser(Users entity) { - _table.Add(new Users - { - UserName = "张三", - Password = "123" - }); + System.Console.WriteLine(entity); + var user = + new Users + { + UserName = entity.UserName, + Password = entity.Password + }; + var list = _users.Add(user); + return list; } + + [HttpPut] + [Route("{id}")] + public Users update(Guid id,Users entity) + { + var user = _users.getById(id); + if(user != null){ + user.UserName = entity.UserName; + user.Password = entity.Password; + _users.Update(user); + } + return user; + } + + [HttpDelete] + [Route("{id}")] + public Users deleteUser(Guid id) + { + var user = _users.getById(id); + if(user != null){ + user.IsDeleted = true; + _users.Update(user); + } + return user; + } + + // public class UserCto + // { + // public string UserName { get; set; } + + // public string Password { get; set; } + + // public bool IsActived { get; set; } + + // public bool IsDeleted { get; set; } + // } } } \ No newline at end of file diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" index fc23443..7106730 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/Startup.cs" @@ -5,10 +5,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using webapi.Domain.Repository; +using webapi.Infrastructure.Db; +using webapi.Infrastructure.Repository; namespace webapi.api { @@ -24,6 +28,11 @@ namespace webapi.api // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + var Sqlserver = Configuration.GetConnectionString("Sqlserver"); + services.AddDbContext(options=>options.UseSqlServer(Sqlserver)); + + // 实现CRUD的依赖注入 + services.AddScoped(typeof(IRepository<>),typeof(EFRepository<>)); services.AddControllers(); } diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" index d9d9a9b..8864025 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.api/appsettings.json" @@ -6,5 +6,8 @@ "Microsoft.Hosting.Lifetime": "Information" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "Sqlserver": "server=.;database=webapi;uid=sa;pwd=123456;" + } } diff --git "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" index 4fca83e..5f5140b 100644 --- "a/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" +++ "b/\351\202\271\346\265\267\345\205\265/lianxi/webapi.domain/Repository/IRepository.cs" @@ -9,7 +9,7 @@ namespace webapi.Domain.Repository IQueryable Table{get;} - void Add(T entity); + T Add(T entity); void Update(T entity); -- Gitee