EFCore通用仓储类

cs 复制代码
using Microsoft.EntityFrameworkCore;
using SmarkParking.Server.IService;
using SmartParking.Server.IEFContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace SmarkParking.Server.Service
{
    public class ServiceBase : IServiceBase
    {

        protected DbContext Context { get; private set; }
        public ServiceBase(IEFContext eFContext)
        {
            Context = eFContext.CreateDBContext();
        }

        public void Commit()
        {
            this.Context.SaveChanges(); // 直接保存就行了
        }

        public void Delete<T>(int Id) where T : class
        {
            T t = this.Find<T>(Id);
            if (t == null) throw new Exception("t is null");    // 如果是空就报错
            this.Context.Set<T>().Remove(t);    // 删除这个
            this.Commit();  // 删完提交
        }

        public void Delete<T>(T t) where T : class
        {
            if (t == null) throw new Exception("t is null");    // 如果是空就报错
            this.Context.Set<T>().Attach(t);    // 尊卑删除这个实体
            this.Context.Set<T>().Remove(t);    // 删除这个
            this.Commit();  // 删完提交
        }

        public void Delete<T>(IEnumerable<T> tList) where T : class
        {
            // 将要删除的实体标记上
            foreach (var t in tList)
            {
                this.Context.Set<T>().Attach(t);    // 要删除这个实体
            }
            this.Context.Set<T>().RemoveRange(tList);    // 删除这个集合
            this.Commit();  // 删完提交
        }

        public T Find<T>(int id) where T : class
        {
            return this.Context.Set<T>().Find(id);
        }

        public T Insert<T>(T t) where T : class
        {
            this.Context.Set<T>().Add(t);   // 添加
            this.Commit();  // 提交
            return t;
        }

        public IEnumerable<T> Insert<T>(IEnumerable<T> tList) where T : class
        {
            this.Context.Set<T>().AddRange(tList);  
            this.Commit();  //
            return tList;
        }

        public IQueryable<T> Query<T>(Expression<Func<T, bool>> funcWhere) where T : class
        {
            return this.Context.Set<T>().Where<T>(funcWhere);
        }

        public void Update<T>(T t) where T : class
        {
            if (t == null) throw new Exception("t is null");

            this.Context.Set<T>().Attach(t);    // 跟踪实体
            this.Context.Entry<T>(t).State = EntityState.Modified;  // 标记为已修改
            this.Commit();
        }

        public void Update<T>(IEnumerable<T> tList) where T : class
        {
            // 跟踪和标记更改
            foreach (var t in tList)
            {
                this.Context.Set<T>().Attach(t);
                this.Context.Entry<T>(t).State = EntityState.Modified;
            }
            this.Commit();
        }

        public virtual void Dispose()
        {
            if (this.Context != null)
                this.Context.Dispose();
        }
    }
}

使用举例子

var users = _loginService.Query<SysUserInfo>(u => u.UserName == username && u.Password == pwd); // 他是Func<T, bool>类型做委托

相关推荐
SHUIPING_YANG13 分钟前
根据用户id自动切换表查询
java·服务器·数据库
爱吃烤鸡翅的酸菜鱼25 分钟前
IDEA高效开发:Database Navigator插件安装与核心使用指南
java·开发语言·数据库·编辑器·intellij-idea·database
超奇电子29 分钟前
阿里云OSS预签名URL上传与临时凭证上传的技术对比分析
数据库·阿里云·云计算
神仙别闹42 分钟前
基于C#+SQL Server实现(Web)学生选课管理系统
前端·数据库·c#
m0_653031361 小时前
PostgreSQL技术大讲堂 - 第97讲:PG数据库编码和区域(locale)答疑解惑
数据库·postgresql
会编程的林俊杰1 小时前
MySQL中的锁有哪些
数据库·mysql
cts6181 小时前
Milvus分布式数据库工作职责
数据库·分布式·milvus
周胡杰1 小时前
鸿蒙加载预置数据库-关系型数据库-如何读取本地/预制数据库
数据库·华为·harmonyos·鸿蒙
布朗克1681 小时前
java常见的jvm内存分析工具
java·jvm·数据库
胡八一1 小时前
SQLite / LiteDB 单文件数据库为何“清空表后仍占几 GB”?——原理解析与空间回收实战
jvm·数据库·sqlite