From 806c3f2c9b76790f4fcf8ba945337ae092e50341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=8F=B6=E5=AD=90?= <123546465> Date: Mon, 8 Jun 2020 14:07:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E5=8F=B6=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WebApi0001/Controllers/TestController.cs" | 27 ++++++ .../Implementation/EfRespository.cs" | 86 +++++++++++++++++++ .../WebApi0001/Interface/IRespository.cs" | 30 +++++++ 3 files changed, 143 insertions(+) create mode 100644 "\345\260\217\345\217\266\345\255\220/WebApi0001/Controllers/TestController.cs" create mode 100644 "\345\260\217\345\217\266\345\255\220/WebApi0001/Implementation/EfRespository.cs" create mode 100644 "\345\260\217\345\217\266\345\255\220/WebApi0001/Interface/IRespository.cs" diff --git "a/\345\260\217\345\217\266\345\255\220/WebApi0001/Controllers/TestController.cs" "b/\345\260\217\345\217\266\345\255\220/WebApi0001/Controllers/TestController.cs" new file mode 100644 index 0000000..8f23bb5 --- /dev/null +++ "b/\345\260\217\345\217\266\345\255\220/WebApi0001/Controllers/TestController.cs" @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Mvc; +using System.Linq; +using WebApi0001.Domain.Model; +using WebApi0001.Helper; +using WebApi0001.Interface; + +namespace WebApi0001.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class TestController : ControllerBase + { + + private readonly IRespository _userReapositoty; + + public TestController(IRespository userRespository) + { + _userReapositoty = userRespository; + } + + public string Get() + { + var list = _userReapositoty.Table.ToList(); + return JsonHelper.SerializeObject(list); + } + } +} diff --git "a/\345\260\217\345\217\266\345\255\220/WebApi0001/Implementation/EfRespository.cs" "b/\345\260\217\345\217\266\345\255\220/WebApi0001/Implementation/EfRespository.cs" new file mode 100644 index 0000000..ad1c628 --- /dev/null +++ "b/\345\260\217\345\217\266\345\255\220/WebApi0001/Implementation/EfRespository.cs" @@ -0,0 +1,86 @@ +using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using WebApi0001.Controllers; +using WebApi0001.Interface; +using WebApplication1.Domain; + +namespace WebApi0001.Implementation +{ + public class EfRespository : IRespository where T : BaseEntity//实现接口 + { + private readonly Admin1000DbContext db; + + private DbSet _entity; + + protected DbSet Entity + { + get + { + + if (_entity == null) + { + _entity = db.Set(); + } + + return _entity; + } + } + + public IQueryable Table + { + get + { + return Entity; + } + } + + public EfRespository(Admin1000DbContext dbContext) + { + db = dbContext; + } + + + public void Delete(T entity) + { + this._entity.Remove(entity); + + db.SaveChanges();//保存修改 + } + + public void Delete(int id) + { + var row = Table.Where(x => x.id == id).FirstOrDefault(); + //调用上面的函数 + Delete(row); + } + + //public IEnumerable GetAllEntity() + //{ + // throw new NotImplementedException(); + //} + + public T GetById(int id) + { + return _entity.Where(x => x.id == id).FirstOrDefault(); + } + + public void Insert(T entity) + { + _entity.Add(entity); + db.SaveChanges(); + } + + public void InsertBulk(IEnumerable list) + { + _entity.AddRange(list); + db.SaveChanges(); + } + + public void Update(T entity) + { + _entity.Update(entity); + db.SaveChanges(); + } + } +} \ No newline at end of file diff --git "a/\345\260\217\345\217\266\345\255\220/WebApi0001/Interface/IRespository.cs" "b/\345\260\217\345\217\266\345\255\220/WebApi0001/Interface/IRespository.cs" new file mode 100644 index 0000000..139f768 --- /dev/null +++ "b/\345\260\217\345\217\266\345\255\220/WebApi0001/Interface/IRespository.cs" @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace WebApi0001.Interface +{ + public interface IRespository + { + IQueryable Table { get; } + + T GetById(int id); + + + //添加 + void Insert(T entity); + + void InsertBulk(IEnumerable list); + + + //更新,修改 + void Update(T entity); + + void Delete(T entity); + + + //根据Id删除 + void Delete(int id); + } +} -- Gitee