From a7439bfdcb0888b7b00ab9151eef4ac2250e6967 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Fri, 12 Apr 2024 09:36:47 +0800 Subject: [PATCH] EF --- .../Controllers/Ghra_GradeController.cs | 9 +- Tiobon.Core.Api/Tiobon.Core.Api.csproj | 1 + Tiobon.Core.DataAccess/ContextFactory.cs | 24 ++++ Tiobon.Core.DataAccess/DataContext.cs | 18 +++ Tiobon.Core.DataAccess/Domain/IBaseCRUDVM.cs | 33 ++++++ .../Domain/Repositories/BaseCRUDVM.cs | 108 ++++++++++++++++++ .../Migrations/DataContextModelSnapshot.cs | 13 +++ .../Tiobon.Core.DataAccess.csproj | 39 +++++++ Tiobon.Core.sln | 6 + 9 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 Tiobon.Core.DataAccess/ContextFactory.cs create mode 100644 Tiobon.Core.DataAccess/DataContext.cs create mode 100644 Tiobon.Core.DataAccess/Domain/IBaseCRUDVM.cs create mode 100644 Tiobon.Core.DataAccess/Domain/Repositories/BaseCRUDVM.cs create mode 100644 Tiobon.Core.DataAccess/Migrations/DataContextModelSnapshot.cs create mode 100644 Tiobon.Core.DataAccess/Tiobon.Core.DataAccess.csproj diff --git a/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs b/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs index 5d5bf019..2a6eb6ce 100644 --- a/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs +++ b/Tiobon.Core.Api/Controllers/Ghra_GradeController.cs @@ -1,4 +1,7 @@ -namespace Tiobon.Core.Api.Controllers +using Microsoft.EntityFrameworkCore; +using Tiobon.Core.DataAccess; + +namespace Tiobon.Core.Api.Controllers { [Route("api/[controller]/[action]")] [ApiController] @@ -23,6 +26,10 @@ [HttpGet] public async Task>> Get([FromFilter] QueryFilter filter) { + + using var _context = ContextFactory.CreateContext(); + var list = await _context.Ghra_Grade.ToListAsync(); + return new MessageModel>() { msg = "获取成功", diff --git a/Tiobon.Core.Api/Tiobon.Core.Api.csproj b/Tiobon.Core.Api/Tiobon.Core.Api.csproj index b5a36322..cfa633c2 100644 --- a/Tiobon.Core.Api/Tiobon.Core.Api.csproj +++ b/Tiobon.Core.Api/Tiobon.Core.Api.csproj @@ -104,6 +104,7 @@ + diff --git a/Tiobon.Core.DataAccess/ContextFactory.cs b/Tiobon.Core.DataAccess/ContextFactory.cs new file mode 100644 index 00000000..90a13b26 --- /dev/null +++ b/Tiobon.Core.DataAccess/ContextFactory.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using Tiobon.Core.Common.DB; + +namespace Tiobon.Core.DataAccess +{ + /// + /// 直接创建 Context + /// + public class ContextFactory + { + /// + /// 创建DbContext + /// + /// + public static DataContext CreateContext() + { + var builder = new DbContextOptionsBuilder(); + + var mainConnetctDb = BaseDBConfig.MutiConnectionString.allDbs.Find(x => x.ConnId == MainDb.CurrentDbConnId); + builder.UseSqlServer(mainConnetctDb.Connection); + return new DataContext(builder.Options); + } + } +} diff --git a/Tiobon.Core.DataAccess/DataContext.cs b/Tiobon.Core.DataAccess/DataContext.cs new file mode 100644 index 00000000..855057e1 --- /dev/null +++ b/Tiobon.Core.DataAccess/DataContext.cs @@ -0,0 +1,18 @@ +using Microsoft.EntityFrameworkCore; +using Tiobon.Core.Model.Models; + +namespace Tiobon.Core.DataAccess +{ + public class DataContext : DbContext + { + + + public virtual DbSet Ghra_Grade { get; set; } + + //占位符 + + public DataContext(DbContextOptions options) : base(options) + { + } + } +} diff --git a/Tiobon.Core.DataAccess/Domain/IBaseCRUDVM.cs b/Tiobon.Core.DataAccess/Domain/IBaseCRUDVM.cs new file mode 100644 index 00000000..1b475da1 --- /dev/null +++ b/Tiobon.Core.DataAccess/Domain/IBaseCRUDVM.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace Tiobon.Core.Domain +{ + public interface IBaseCRUDVM where TModel : class + { + IEnumerable Get(); + TModel Get(Expression> express = null); + + TModel GetById(object id); + Task GetByIdAsync(object id); + + void DoAdd(TModel model); + + Task DoAddAsync(TModel model); + + void DoDelete(object id); + + Task DoDeleteAsync(Guid id, Guid? updateById = null); + + void DoRealDelete(object id); + + Task DoRealDeleteAsync(object id); + + void DoUpdate(TModel model); + + Task DoUpdateAsync(TModel model); + Task GetAsync(Expression> express); + } +} diff --git a/Tiobon.Core.DataAccess/Domain/Repositories/BaseCRUDVM.cs b/Tiobon.Core.DataAccess/Domain/Repositories/BaseCRUDVM.cs new file mode 100644 index 00000000..2b450b78 --- /dev/null +++ b/Tiobon.Core.DataAccess/Domain/Repositories/BaseCRUDVM.cs @@ -0,0 +1,108 @@ +using Tiobon.Core.DataAccess; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; + +namespace Tiobon.Core.Domain +{ + public class BaseCRUDVM : IBaseCRUDVM where TModel : class + { + private readonly DataContext _context; + internal DbSet dbSet; + + public BaseCRUDVM(DataContext context) + { + _context = context; + this.dbSet = _context.Set(); + } + + IEnumerable IBaseCRUDVM.Get() + { + return dbSet.ToList(); + } + + TModel IBaseCRUDVM.Get(Expression> express) + { + return dbSet.Where(express).FirstOrDefault(); + } + + async Task IBaseCRUDVM.GetAsync(Expression> express) + { + return await dbSet.Where(express).FirstOrDefaultAsync(); + } + + public TModel GetById(object id) + { + return dbSet.Find(id); + } + + public async Task GetByIdAsync(object id) + { + return await dbSet.FindAsync(id); + } + + public void DoAdd(TModel model) + { + _context.Entry(model).CurrentValues["IsDeleted"] = false; + _context.Entry(model).CurrentValues["AuditStatus"] = "Add"; + _context.Add(model); + _context.SaveChanges(); + } + + public async Task DoAddAsync(TModel model) + { + _context.Entry(model).CurrentValues["IsDeleted"] = false; + _context.Entry(model).CurrentValues["AuditStatus"] = "Add"; + await _context.AddAsync(model); + await _context.SaveChangesAsync(); + } + + public void DoDelete(object id) + { + var query = dbSet.Find(id); + _context.Entry(query).CurrentValues["IsDeleted"] = true; + _context.Update(query); + _context.SaveChanges(); + } + + public async Task DoDeleteAsync(Guid id, Guid? updateById = null) + { + var query = await dbSet.FindAsync(id); + _context.Entry(query).CurrentValues["IsDeleted"] = true; + _context.Entry(query).CurrentValues["UpdateBy"] = updateById ?? null; + _context.Entry(query).CurrentValues["UpdateTime"] = DateTime.Now; + await _context.SaveChangesAsync(); + } + + public void DoRealDelete(object id) + { + var query = dbSet.Find(id); + _context.Remove(query); + _context.SaveChanges(); + } + + public async Task DoRealDeleteAsync(object id) + { + var query = await dbSet.FindAsync(id); + _context.Remove(query); + await _context.SaveChangesAsync(); + } + + public void DoUpdate(TModel model) + { + _context.Update(model); + _context.SaveChanges(); + } + + public async Task DoUpdateAsync(TModel model) + { + _context.Update(model); + await _context.SaveChangesAsync(); + } + + + } +} diff --git a/Tiobon.Core.DataAccess/Migrations/DataContextModelSnapshot.cs b/Tiobon.Core.DataAccess/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 00000000..8266f829 --- /dev/null +++ b/Tiobon.Core.DataAccess/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; + +namespace Tiobon.Core.DataAccess.Migrations +{ + [DbContext(typeof(DataContext))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { + } + } +} diff --git a/Tiobon.Core.DataAccess/Tiobon.Core.DataAccess.csproj b/Tiobon.Core.DataAccess/Tiobon.Core.DataAccess.csproj new file mode 100644 index 00000000..e93aad94 --- /dev/null +++ b/Tiobon.Core.DataAccess/Tiobon.Core.DataAccess.csproj @@ -0,0 +1,39 @@ + + + + net8.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Tiobon.Core.sln b/Tiobon.Core.sln index 20f3a9de..57ea08d0 100644 --- a/Tiobon.Core.sln +++ b/Tiobon.Core.sln @@ -60,6 +60,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tiobon.Core.EventBus", "Tio EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tiobon.Core.Tests", "Tiobon.Core.Tests\Tiobon.Core.Tests.csproj", "{1CD6CC3C-D58C-47D1-9F9A-671BB9261A46}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tiobon.Core.DataAccess", "Tiobon.Core.DataAccess\Tiobon.Core.DataAccess.csproj", "{44B3465C-70F2-420F-8B13-50BC264AD0EA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -126,6 +128,10 @@ Global {1CD6CC3C-D58C-47D1-9F9A-671BB9261A46}.Debug|Any CPU.Build.0 = Debug|Any CPU {1CD6CC3C-D58C-47D1-9F9A-671BB9261A46}.Release|Any CPU.ActiveCfg = Release|Any CPU {1CD6CC3C-D58C-47D1-9F9A-671BB9261A46}.Release|Any CPU.Build.0 = Release|Any CPU + {44B3465C-70F2-420F-8B13-50BC264AD0EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {44B3465C-70F2-420F-8B13-50BC264AD0EA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {44B3465C-70F2-420F-8B13-50BC264AD0EA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {44B3465C-70F2-420F-8B13-50BC264AD0EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE