代码拉取完成,页面将自动刷新
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();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。