EF 用到的两个类

EFDBEntities类

using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Configuration;
using System.Data.Entity;
using System.Linq;

namespace HRMS.Models
{
    public partial class EFDBEntities : DbContext
    {
        private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();

        public EFDBEntities()
            : base(connString)
        {
        }

        public virtual DbSet<PD_Bank> PD_Bank { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
}

 

 

 

 

DAL类

 

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HRMS.Models;


namespace HRMS.SQLServerDAL
{

    /// <summary>
    /// T为引用类型,且有个无参的构造函数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseDAL<T> where T : class, new()
    {
        private EFDBEntities db = new EFDBEntities();

        #region 通用方法

        public int Insert(T model)
        {
            db.Set<T>().Add(model);
            return db.SaveChanges();
        }

        public int DeleteByPrimaryKey(T model)
        {
            db.Set<T>().Attach(model);
            db.Set<T>().Remove(model);
            return db.SaveChanges();
        }

        public int UpdateByPrimaryKey(T model)
        {
            db.Set<T>().Attach(model);
            db.Entry(model).State = EntityState.Modified;
            return db.SaveChanges();
        }

        public List<T> GetAllList()
        {
            return db.Set<T>().Select(p => p).ToList();
        }

        public int InsertRange(List<T> list)
        {
            db.Set<T>().AddRange(list);
            return db.SaveChanges();
        }

        public int DeleteRange(List<T> list)
        {
            db.Set<T>().RemoveRange(list);
            return db.SaveChanges();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="TKey">排序的字段的类型</typeparam>
        /// <param name="orderLambda">按上面方式排序,升序还是降序</param>
        /// <param name="whereLambda">分页的Where条件</param>
        /// <param name="pagesize">每页多少条数据</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <returns></returns>
        public List<T> GetPageList<TKey>(Expression<Func<T, TKey>> orderLambda, Expression<Func<T, bool>> whereLambda, int pagesize, int pageIndex)
        {
            return db.Set<T>().Where(whereLambda)
                .OrderBy(orderLambda)
                .Skip((pageIndex - 1) * pagesize)
                .Take(pagesize)
                .Select(u => u).ToList();
        }


        /// <summary>
        /// 反射实现两个类的对象之间相同属性的值的复制
        /// 适用于没有新建实体之间
        /// </summary>
        /// <typeparam name="D">返回的实体</typeparam>
        /// <typeparam name="S">数据源实体</typeparam>
        /// <param name="d">返回的实体</param>
        /// <param name="s">数据源实体</param>
        /// <returns></returns>
        public D MapperToModel<D, S>(D d, S s)
        {
            try
            {
                var Types = s.GetType();//获得类型  
                var Typed = typeof(D);
                foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段  
                {
                    foreach (PropertyInfo dp in Typed.GetProperties())
                    {
                        if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同  
                        {
                            dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性  
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return d;
        }


        /// <summary>
        /// 反射实现两个类的对象之间相同属性的值的复制
        /// 适用于初始化新实体
        /// </summary>
        /// <typeparam name="D">返回的实体</typeparam>
        /// <typeparam name="S">数据源实体</typeparam>
        /// <param name="s">数据源实体</param>
        /// <returns>返回的新实体</returns>
        public D Mapper<D, S>(S s)
        {
            D d = Activator.CreateInstance<D>(); //构造新实例
            try
            {
                var Types = s.GetType();//获得类型  
                var Typed = typeof(D);
                foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段  
                {
                    foreach (PropertyInfo dp in Typed.GetProperties())
                    {
                        if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同  
                        {
                            dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性  
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return d;
        }

        #endregion



    }
}

 

BLL类

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using HRMS.Models;
using HRMS.SQLServerDAL;

namespace HRMS.BLL
{
    public class BaseBLL<T> where T : class, new()
    {
        BaseDAL<T> dal = new BaseDAL<T>();

        public int Insert(T model)
        {
            return dal.Insert(model);
        }

        public int DeleteByPrimaryKey(T model)
        {
            return dal.DeleteByPrimaryKey(model);
        }

        public int UpdateByPrimaryKey(T model)
        {
            return dal.UpdateByPrimaryKey(model);
        }

        public List<T> GetAllList()
        {
            return dal.GetAllList();
        }

        public int InsertRange(List<T> list)
        {
            return dal.InsertRange(list);
        }

        public int DeleteRange(List<T> list)
        {
            return dal.DeleteRange(list);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="TKey">排序的字段的类型</typeparam>
        /// <param name="orderLambda">按上面方式排序,升序还是降序</param>
        /// <param name="whereLambda">分页的Where条件</param>
        /// <param name="pagesize">每页多少条数据</param>
        /// <param name="pageIndex">当前第几页</param>
        /// <returns></returns>
        public List<T> GetPageList<TKey>(Expression<Func<T, TKey>> orderLambda, Expression<Func<T, bool>> whereLambda, int pagesize, int pageIndex)
        {
            return dal.GetPageList(orderLambda, whereLambda, pagesize, pageIndex);
        }

        /// <summary>
        /// 反射实现两个类的对象之间相同属性的值的复制
        /// 适用于没有新建实体之间
        /// </summary>
        /// <typeparam name="D">返回的实体</typeparam>
        /// <typeparam name="S">数据源实体</typeparam>
        /// <param name="d">返回的实体</param>
        /// <param name="s">数据源实体</param>
        /// <returns></returns>
        public D MapperToModel<D, S>(D d, S s)
        {
            return dal.MapperToModel(d, s);
        }

        /// <summary>
        /// 反射实现两个类的对象之间相同属性的值的复制
        /// 适用于初始化新实体
        /// </summary>
        /// <typeparam name="D">返回的实体</typeparam>
        /// <typeparam name="S">数据源实体</typeparam>
        /// <param name="s">数据源实体</param>
        /// <returns>返回的新实体</returns>
        public D Mapper<D, S>(S s)
        {
            return dal.Mapper<D, S>(s);
        }


    }
}

 

 

using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.Infrastructure;using System.Linq;using System.Linq.Expressions;using System.Reflection;using System.Text;using System.Threading.Tasks;using HRMS.Models;

namespace HRMS.SQLServerDAL{
    /// <summary>    /// T为引用类型,且有个无参的构造函数    /// </summary>    /// <typeparam name="T"></typeparam>    public class BaseDAL<T> where T : class, new()    {        private EFDBEntities db = new EFDBEntities();
        #region 通用方法
        public int Insert(T model)        {            db.Set<T>().Add(model);            return db.SaveChanges();        }
        public int DeleteByPrimaryKey(T model)        {            db.Set<T>().Attach(model);            db.Set<T>().Remove(model);            return db.SaveChanges();        }
        public int UpdateByPrimaryKey(T model)        {            db.Set<T>().Attach(model);            db.Entry(model).State = EntityState.Modified;            return db.SaveChanges();        }
        public List<T> GetAllList()        {            return db.Set<T>().Select(p => p).ToList();        }
        public int InsertRange(List<T> list)        {            db.Set<T>().AddRange(list);            return db.SaveChanges();        }
        public int DeleteRange(List<T> list)        {            db.Set<T>().RemoveRange(list);            return db.SaveChanges();        }
        /// <summary>        /// 分页查询        /// </summary>        /// <typeparam name="TKey">排序的字段的类型</typeparam>        /// <param name="orderLambda">按上面方式排序,升序还是降序</param>        /// <param name="whereLambda">分页的Where条件</param>        /// <param name="pagesize">每页多少条数据</param>        /// <param name="pageIndex">当前第几页</param>        /// <returns></returns>        public List<T> GetPageList<TKey>(Expression<Func<T, TKey>> orderLambda, Expression<Func<T, bool>> whereLambda, int pagesize, int pageIndex)        {            return db.Set<T>().Where(whereLambda)                .OrderBy(orderLambda)                .Skip((pageIndex - 1) * pagesize)                .Take(pagesize)                .Select(u => u).ToList();        }

        /// <summary>        /// 反射实现两个类的对象之间相同属性的值的复制        /// 适用于没有新建实体之间        /// </summary>        /// <typeparam name="D">返回的实体</typeparam>        /// <typeparam name="S">数据源实体</typeparam>        /// <param name="d">返回的实体</param>        /// <param name="s">数据源实体</param>        /// <returns></returns>        public D MapperToModel<D, S>(D d, S s)        {            try            {                var Types = s.GetType();//获得类型                  var Typed = typeof(D);                foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段                  {                    foreach (PropertyInfo dp in Typed.GetProperties())                    {                        if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同                          {                            dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性                          }                    }                }            }            catch (Exception ex)            {                throw ex;            }            return d;        }

        /// <summary>        /// 反射实现两个类的对象之间相同属性的值的复制        /// 适用于初始化新实体        /// </summary>        /// <typeparam name="D">返回的实体</typeparam>        /// <typeparam name="S">数据源实体</typeparam>        /// <param name="s">数据源实体</param>        /// <returns>返回的新实体</returns>        public D Mapper<D, S>(S s)        {            D d = Activator.CreateInstance<D>(); //构造新实例            try            {                var Types = s.GetType();//获得类型                  var Typed = typeof(D);                foreach (PropertyInfo sp in Types.GetProperties())//获得类型的属性字段                  {                    foreach (PropertyInfo dp in Typed.GetProperties())                    {                        if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同                          {                            dp.SetValue(d, sp.GetValue(s, null), null);//获得s对象属性的值复制给d对象的属性                          }                    }                }            }            catch (Exception ex)            {                throw ex;            }            return d;        }
        #endregion


    }}

posted @ 2020-12-27 17:10  南秦岭  阅读(129)  评论(0编辑  收藏  举报