zoukankan      html  css  js  c++  java
  • EfRepository

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Text;
    using System.Data.Entity;

    class EfRepository
    {
    private DbContext context;

    #region 构造函数
    public EfRepository(DbContext dbcontext)
    {
    context = dbcontext;
    }
    #endregion

    #region IRepository

    public IQueryable<T> All<T>() where T : class
    {
    return context.Set<T>().AsNoTracking();
    }

    public void Update<T>(T entity) where T : class
    {
    var entry = context.Entry(entity);
    if (entry.State == EntityState.Detached)
    {
    context.Set<T>().Attach(entity);
    }
    entry.State = EntityState.Modified;
    }

    public void Insert<T>(T entity) where T : class
    {
    context.Set<T>().Add(entity);
    }

    public void Delete<T>(T entity) where T : class
    {
    var entry = context.Entry(entity);
    if (entry.State == EntityState.Detached)
    {
    context.Set<T>().Attach(entity);
    }
    entry.State = EntityState.Deleted;
    context.Set<T>().Remove(entity);
    }

    public void Delete<T>(Expression<Func<T, bool>> conditions) where T : class
    {
    var list = Find<T>(conditions);
    foreach (var item in list)
    {
    Delete<T>(item);
    }

    }

    public T Get<T>(Expression<Func<T, bool>> conditions) where T : class
    {
    return All<T>().FirstOrDefault(conditions);
    }

    public List<T> Find<T>(Expression<Func<T, bool>> conditions = null) where T : class
    {
    if (conditions != null)
    {
    return All<T>().Where(conditions).ToList();
    }
    else
    {
    return All<T>().ToList();
    }

    }

    public List<T> Find<T, S>(Expression<Func<T, bool>> conditions, Expression<Func<T, S>> orderBy, int pageSize, int pageIndex, out int totalCount) where T : class
    {
    var queryList = conditions == null ?
    All<T>() :
    All<T>().Where(conditions);

    totalCount = queryList.Count();

    return queryList.OrderByDescending(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
    }

    public List<T> SqlQuery<T>(string sql)
    {
    return context.Database.SqlQuery<T>(sql).ToList();
    }

    public int ExecuteSqlCommand(string sql)
    {
    return context.Database.ExecuteSqlCommand(sql);
    }

    public int SaveChanges()
    {
    return context.SaveChanges();
    }

    public void Dispose()
    {
    context.Dispose();
    }

    public long GetNextSequenceValue(string sequenceName)
    {
    var rawQuery = context.Database.SqlQuery<long>(string.Format("SELECT NEXT VALUE FOR {0}", sequenceName)).ToList();
    long nextVal = rawQuery.Single();
    return nextVal;
    }


    #endregion
    }

  • 相关阅读:
    BZOJ 4236~4247 题解
    OCUI界面设计:滚动视图与分页控件初探
    大数乘法
    Effective C++ 11-17
    [Matlab+C/C++] 读写二进制文件
    Java 反射 方法调用
    如何使用LaTeX让自己不乱?
    LaTeX 相对于 Word 有什么优势?
    你在发表理科学术文章过程中有哪些经验值得借鉴?
    破译手势在对话中的意义
  • 原文地址:https://www.cnblogs.com/pengzhihua/p/7243319.html
Copyright © 2011-2022 走看看