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>类型做委托

相关推荐
骑着王八撵玉兔2 小时前
【性能优化与架构调优(二)】高性能数据库设计与优化
数据库·性能优化·架构
想要入门的程序猿3 小时前
Qt写入excel
数据库·qt·excel
Q_970956393 小时前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
Wyc724093 小时前
Maven
java·数据库·maven
程序猿小D3 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
羊小猪~~3 小时前
数据库学习笔记(十七)--触发器的使用
数据库·人工智能·后端·sql·深度学习·mysql·考研
背太阳的牧羊人4 小时前
Neo4j 的向量搜索(Neo4jVector)和常见的向量数据库(比如 Milvus、Qdrant)之间的区别与联系
数据库·neo4j·milvus
liulun4 小时前
在浏览器中使用SQLite(官方sqlite3.wasm)
数据库·sqlite·wasm
IT项目管理5 小时前
达梦数据库DMHS介绍及安装部署
linux·数据库
你都会上树?5 小时前
MySQL MVCC 详解
数据库·mysql