public class BaseDal<T> where T : class, new()
    {
        DataModelContainer db = new DataModelContainer();

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IQueryable<T> GetEntities(Expression<Func<T, bool>> where)
        {
            return db.Set<T>().Where(where).AsQueryable();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        /// <param name="where"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetPageEntities<S>(int pageSize, int pageIndex, out int count, Expression<Func<T, bool>> where, Expression<Func<T, S>> orderBy, bool isAsc)
        {
            count = db.Set<T>().Where(where).Count();
            if (isAsc)
            {
                var temp = db.Set<T>().Where(where).OrderBy<T, S>(orderBy).Skip(pageSize).Take(pageSize * (pageIndex - 1)).AsQueryable();
                return temp;
            }
            else
            {
                var temp = db.Set<T>().Where(where).OrderByDescending<T, S>(orderBy).Skip(pageSize).Take(pageSize * (pageIndex - 1)).AsQueryable();
                return temp;
            }
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Add(T entity)
        {
            db.Set<T>().Add(entity);
            db.SaveChanges();
            return entity;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            db.Entry(entity).State = EntityState.Deleted;
            return db.SaveChanges() > 0;
        }
    }

EFBaseDal的更多相关文章

  1. EFBaseDal新增删除方法

    public T Delete(int id )        {            var entity = db.Set<T>().Find(id);            T t ...

  2. Log2Net组件代码详解(附开源代码)

    上一篇,我们介绍了Log2Net的需求和整体框架,我们接下来介绍我们是如何用代码实现Log2Net组件的功能的. 一.整体介绍 Log2Net组件本身是一个Dll,供其他系统调用. 本部分由以下几部分 ...

随机推荐

  1. 【设计模式 - 24】之访问者模式(Visitor)

    1      模式简介 访问者模式的定义: 访问者模式将数据结构与数据操作进行了分离,解决了稳定的数据结构和易变的数据操作的耦合问题. 访问者模式的优点: 1)        符合单一职责原则: 2) ...

  2. 【学习】ABAP OLE 对EXCEL的处理

    原文:http://blog.sina.com.cn/s/blog_7229b9c00100opx2.html -------------------------------------------- ...

  3. PowerShell常用的.Net 、COM对象(New-Object、Assembly)、加载程序集

    #新建随机数对象实例:$Ran = New-Object System.Random$Ran.NextDouble() 有时候,要使用的实例的类保存在独立的库文件中,PowerShell默认未加载,会 ...

  4. Nginx平台构架 分类: Nginx 2015-07-13 10:55 205人阅读 评论(0) 收藏

    深入理解Nginx模块发开与架构解析读书笔记. nginx在启动后,在unix系统中会以daemon的方式(可以手动关闭 nginx.conf daemon off)在后台运行,后台进程包含一个mas ...

  5. [Javascript] MetaProgramming: function name

    Each function should have a 'name' property. It can be anonymous, empty, the same as function name, ...

  6. 源泉书签,助您管理海量收藏。www.yuanquanshuqian.com,今日更新:多标签功能已实现

    源泉书签.助您管理海量收藏.www.yuanquanshuqian.com,今日更新:多标签功能已实现

  7. Director Scene Layer and Sprite

    Scenes Director Layers Multiple Layers Example: Sprites References Scenes A scene (implemented with ...

  8. sublime text 3.0使用

    # 快捷键    //未完待续 ctrl+p : 文件快速搜索 Ctrl+D : 选词 (按住-继续选择下个相同的字符串) ctrl+L : 选择整行(按住-继续选择下行,即按住ctrl不放按一次L则 ...

  9. windows系统 安装MongoDB 32位

    本篇文章记录了我在win7 32位下安装MongoDB的步骤,以作记录. 下文的安装方法参考了以下博文: http://www.cnblogs.com/lzrabbit/p/3682510.html ...

  10. HDU-1020(水题)

    Encoding Problem Description Given a string containing only 'A' - 'Z', we could encode it using the ...