标签 ef6.0+mvc5+easyui 下的文章

(EF6.0+MVC5+EasyUI多层架构)CURD数据层封装DAL

多层架构:面向接口。
DAL层封装,BaseDal.cs

using BookShop.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookShop.DAL
{
    public class BaseDal<T> where T:class,new()
    {
        //book_shop3Entities Db = new book_shop3Entities();
        DbContext Db = DBContextFactory.CreateDbContext();//线程唯一
        /// <summary>
        /// 条件查询数据
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
        {
            return Db.Set<T>().Where(whereLambda);
        }
        /// <summary>
        /// 翻页查询
        /// </summary>
        /// <typeparam name="s">排序字段类型</typeparam>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderByLambda">排序字段,类型</param>
        /// <param name="isAsc">升/降序</param>
        /// <returns></returns>
        public IQueryable<T> LoadPageEntities<s>(int pageSize, int pageIndex, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderByLambda, bool isAsc)
        {
            var temp = Db.Set<T>().Where(whereLambda);
            totalCount = temp.Count();
            if (isAsc)
            {
                temp = temp.OrderBy<T, s>(orderByLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            }
            else
            {
                temp = temp.OrderByDescending<T, s>(orderByLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            }
            return temp;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public bool DeleteEntity(T Entity)
        {
            Db.Entry<T>(Entity).State = System.Data.Entity.EntityState.Deleted;
            //return Db.SaveChanges() > 0;
            return true;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public bool EditEntity(T Entity)
        {
            Db.Entry<T>(Entity).State = System.Data.Entity.EntityState.Modified;
            //return Db.SaveChanges() > 0;
            return true;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public T AddEntity(T Entity)
        {
            Db.Set<T>().Add(Entity);
            //Db.SaveChanges();
            return Entity;
        }
    }
}

System.Linq.Expressions.Expression<Func<T, bool>> whereLambda: 参数类型为委托,
使用形式ef.UserInfo.where(u=>u.ID==23)


linq形式:

 var userinfo = from u in u.UserInfo
                    where u.ID==23
                    select u

Set():争对上下文中的给定类型的实例,返回DbSet实例。