1 Star 0 Fork 0

siyisiyue/SpreadJsDemoApi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
EFHelper.cs 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
siyisiyue 提交于 2021-03-10 16:10 . 添加项目文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace SpreadJsDemoApi
{
public class EFHelper
{
private readonly SpreadContext _db;
public EFHelper(SpreadContext context)
{
_db = context;
}
public virtual IQueryable<TEntity> Quyery<TEntity>() where TEntity : class
{
return _db.Set<TEntity>();
}
/// <summary>
/// 查询单个实体
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="pre"></param>
/// <returns></returns>
public virtual IQueryable<TEntity> Quyery<TEntity>(Expression<Func<TEntity, bool>> pre) where TEntity : class
{
return _db.Set<TEntity>().Where(pre);
}
/// <summary>
/// 根据ID查询单个实体
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="pre"></param>
/// <returns></returns>
public virtual TEntity QuyeryByID<TEntity>(int id) where TEntity : class, BaseEntity
{
return _db.Set<TEntity>().Where(a => a.Id.Equals(id)).First();
}
/// <summary>
/// 添加实体
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="entity"></param>
public virtual void Add<TEntity>(TEntity entity) where TEntity : class, BaseEntity
{
_db.Set<TEntity>().Add(entity);
}
/// <summary>
/// 批量添加实体
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="listEntity"></param>
public virtual void Add<TEntity>(List<TEntity> listEntity) where TEntity : class, BaseEntity
{
_db.Set<TEntity>().AddRange(listEntity);
}
/// <summary>
/// 根据传入条件删除数据
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="pre"></param>
public virtual void Delete<TEntity>(Expression<Func<TEntity, bool>> pre) where TEntity : class
{
var dele = this.Quyery(pre).FirstOrDefault();
if (dele != null)
this.Delete(dele);
}
/// <summary>
/// 根据实体标记删除
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="entity"></param>
public virtual void Delete<TEntity>(TEntity entity) where TEntity : class
{
_db.Set<TEntity>().Remove(entity);
}
/// <summary>
/// 批量标记删除
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="listEntity"></param>
public virtual void Delete<TEntity>(List<TEntity> listEntity) where TEntity : class, BaseEntity
{
_db.Set<TEntity>().RemoveRange(listEntity);
}
/// <summary>
/// 保存数据到数据库
/// </summary>
public virtual void SaveChanges()
{
_db.SaveChanges();
_db.Dispose();
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/siyisiyue/spread-js-demo-api.git
[email protected]:siyisiyue/spread-js-demo-api.git
siyisiyue
spread-js-demo-api
SpreadJsDemoApi
master

搜索帮助