数据库
首页 > 数据库> > EF Core手动创建实体类操作数据库

EF Core手动创建实体类操作数据库

作者:互联网

业务场景

技术实现

虚空代码

实际开发

总结事项

附录代码

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace KZDBMS.EFCoreModule.DataHelper
{
    public class EFHelper
    {
        public DbContext DatabaseContext { get; set; }

        public EFHelper(DbContext dbContext)
        {
            this.DatabaseContext = dbContext;
        }

        //protected DbContext DatabaseContext
        //{
        //    get
        //    { //为了保证线程唯一性,用线程槽存放
        //        // DbContext dbContext = CallContext.GetData("dbContext") as DbContext;

        //        if (dbContext == null)
        //        {
        //            //为空则创建新的上下文并放入线程槽
        //            //dbContext = new MPMS20190522Entities();
        //            dbContext = new AHDCEntities();
        //            CallContext.SetData("dbContext", dbContext);
        //        }

        //        return dbContext;
        //    }
        //}

        /// <summary>
        /// 查询过滤
        /// </summary>
        /// <typeparam name="C"></typeparam>
        /// <param name="whereLambda"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public IQueryable<T> GetAllEntities<T>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda) where T : class, new()
        {
            return DatabaseContext.Set<T>().Where(whereLambda);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> SelectByPage<T, C>(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, C>> orderBy, int pageIndex, int pageSize, bool isAsc, out int totalCount) where T : class, new()
        {
            var temp = DatabaseContext.Set<T>().Where<T>(whereLambda);
            totalCount = temp.Count();

            if (isAsc)
            {
                //升序取,取得时候跟了泛型
                temp = temp.OrderBy<T, C>(orderBy).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            }
            else
            {
                //降序取,取得时候没跟泛型
                temp = temp.OrderByDescending<T, C>(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            }

            return temp;
        }

        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Add<T>(T entity) where T : class, new()
        {
            //DbContext.Entry<T>(entity).State = EntityState.Added;
            //return DbContext.SaveChanges() > 0;

            DatabaseContext.Set<T>().Add(entity);
            //DbContext.SaveChanges();  //注释掉此处保存操作,改为统一保存
            return entity;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete<T>(T entity) where T : class, new()
        {
            //DbContext.Entry<T>(entity).State = EntityState.Deleted;
            DatabaseContext.Set<T>().Remove(entity);

            //return DbContext.SaveChanges() > 0;   //注释掉此处保存操作,改为统一保存
            return true;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update<T>(T entity) where T : class, new()
        {
            DatabaseContext.Entry<T>(entity).State = EntityState.Modified;

            //return DbContext.SaveChanges() > 0;   //注释掉此处保存操作,改为统一保存
            return true;
        }

        /// <summary>
        /// 保存所作的更改
        /// </summary>
        /// <returns></returns>
        public bool SaveChanges()
        {
            return DatabaseContext.SaveChanges() > 0;
        }
    }
}

参考文献

标签:Core,实体类,return,EF,DbContext,entity,DatabaseContext,dbContext,public
来源: https://www.cnblogs.com/chaliceX/p/15845241.html