• EntityFramework Core 封装


    public class EntityBaseRepository<T> : IEntityBaseRepository<T>

    where T : class, IEntityBase, new()

    {

    private SchedulerContext _context;

    #region Properties

    public EntityBaseRepository(SchedulerContext context)

    {

    _context = context;

    }

    #endregion

    public virtual IEnumerable<T> GetAll()

    {

    return _context.Set<T>().AsEnumerable();

    }

    public virtual int Count()

    {

    return _context.Set<T>().Count();

    }

    public virtual IEnumerable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)

    {

    IQueryable<T> query = _context.Set<T>();

    foreach (var includeProperty in includeProperties)

    {

    query = query.Include(includeProperty);

    }

    return query.AsEnumerable();

    }

    public T GetSingle(int id)

    {

    return _context.Set<T>().FirstOrDefault(x => x.Id == id);

    }

    public T GetSingle(Expression<Func<T, bool>> predicate)

    {

    return _context.Set<T>().FirstOrDefault(predicate);

    }

    public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)

    {

    IQueryable<T> query = _context.Set<T>();

    foreach (var includeProperty in includeProperties)

    {

    query = query.Include(includeProperty);

    }

    return query.Where(predicate).FirstOrDefault();

    }

    public virtual IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate)

    {

    return _context.Set<T>().Where(predicate);

    }

    public virtual void Add(T entity)

    {

    EntityEntry dbEntityEntry = _context.Entry<T>(entity);

    _context.Set<T>().Add(entity);

    }

    public virtual void Update(T entity)

    {

    EntityEntry dbEntityEntry = _context.Entry<T>(entity);

    dbEntityEntry.State = EntityState.Modified;

    }

    public virtual void Delete(T entity)

    {

    EntityEntry dbEntityEntry = _context.Entry<T>(entity);

    dbEntityEntry.State = EntityState.Deleted;

    }

    public virtual void DeleteWhere(Expression<Func<T, bool>> predicate)

    {

    IEnumerable<T> entities = _context.Set<T>().Where(predicate);

    foreach(var entity in entities)

    {

    _context.Entry<T>(entity).State = EntityState.Deleted;

    }

    }

    public virtual void Commit()

    {

    _context.SaveChanges();

    }

    }

  • 相关阅读:
    谁主沉浮
    东野圭吾--嫌疑人X的献身读后感
    (三)Spring 依赖注入
    (二) Spring项目的搭建
    (一) Spring基础概述
    spring boot 运行jsp原理分析
    Java-01背包问题-动态规划-递归和非递归实现
    博客移至CSDN
    Navicat Premium 连接Oracle登入时候报ORA-12638: 身份证明检索失败的解决办法
    oracle 可以连接数据库,vs连不上. 报错提示:ORA-12154: TNS: 无法解析指定的连接标识符
  • 原文地址:https://www.cnblogs.com/94cool/p/5912088.html
Copyright © 2020-2023  润新知