EF Core增删改查基类

    public class BaseServer
    {
    	//上下文
        private readonly DataContext Context;
        protected LogServer LogServer { get; }
		
		//依赖注入
        protected BaseServer(DataContext dataContext, LogServer logServer)
        {
            this.Context = dataContext;
            this.LogServer = logServer;
        }
		
		//查找全部
        protected List<T> GetAll<T>() where T : class
        {
            return Context.Set<T>().ToList();
        }
		
		//根据主键查找
        protected T GetDataByKey<T>(object key1, object key2, object key3) where T : class
        {
            return Context.Set<T>().Find(key1, key2, key3);
        }
		
		//根据Lambda表达式查找
        protected IEnumerable<T> GetDataByLambda<T>(Expression<Func<T, bool>> whereLambda) where T : class
        {
            try
            {
                return Context.Set<T>().Where(whereLambda);
            }
            catch (Exception ex)
            {
                LogServer.LogErr(ex.ToString());
                return null;
            }
        }

		//修改数据
        protected bool Update<T>(T newData) where T : class
        {
            try
            {
                Context.Set<T>().Attach(newData);
                this.Context.Entry(newData).State = EntityState.Modified;
                Context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                LogServer.LogErr(ex.ToString());
                return false;
            }

        }
		
		//添加数据
        protected bool Add<T>(IEnumerable<T> newData) where T : class
        {
            try
            {
                Context.Set<T>().AddRange(newData);
                Context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                LogServer.LogErr(ex.ToString());
                return false;
            }
        }
		
		//根据主键进行数据删除
        protected bool RemoveByKey<T>(object key1, object key2, object key3) where T : class
        {
            try
            {
                var v = GetDataByKey<T>(key1, key2, key3);
                Context.Set<T>().Remove(v);
                Context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                LogServer.LogErr(ex.ToString());
                return false;
            }
        }
		
		//添加数据
        protected bool Add<T>(T newData) where T : class
        {
            try
            {
                Context.Set<T>().Add(newData);
                Context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                LogServer.LogErr(ex.ToString());
                return false;
            }
        }
		
		//查找数据根据sql
        protected List<T> GetDataBySql<T>(string sql) where T :class
        {
            try
            {
                return Context.Set<T>().FromSql(sql).ToList();
            }
            catch (Exception ex)
            {
                LogServer.LogErr("GetDataBySql:" + ex.ToString());
                return null;
            }
        }
		
		//执行存储过程
        protected bool RunProcedure(string sql)
        {
            try
            {
                Context.Database.ExecuteSqlCommand(sql);
                return true;
            }
            catch (Exception ex)
            {
                LogServer.LogErr("RunProcedure:" + ex.ToString());
                return false;
            }
        }
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在ThinkPHP的DAO层中,可以通过建立一个公共的基类来实现增删查的功能。 首先,在DAO层中建立一个BaseDao类,该类包含了通用的增删查操作。例如: ``` namespace app\common\dao; use think\Db; class BaseDao { protected $table; // 数据表名 public function __construct($table) { $this->table = $table; } // 新增记录 public function create($data) { Db::table($this->table)->insert($data); } // 删除记录 public function delete($condition) { Db::table($this->table)->where($condition)->delete(); } // 更新记录 public function update($condition, $data) { Db::table($this->table)->where($condition)->update($data); } // 查询单条记录 public function findOne($condition) { return Db::table($this->table)->where($condition)->find(); } // 查询多条记录 public function findAll($condition) { return Db::table($this->table)->where($condition)->select(); } } ``` 然后,在其他DAO类中继承BaseDao类,就可以直接调用BaseDao中的增删查方法。例如,在UserDao中继承BaseDao类: ``` namespace app\common\dao; class UserDao extends BaseDao { public function __construct() { parent::__construct('user'); // 数据表名为user } } ``` 这样,通过调用UserDao中继承自BaseDao的方法,就可以实现对user表的增删查操作。例如: ``` $userDao = new UserDao(); // 新增记录 $data = ['name' => 'Tom', 'age' => 20]; $userDao->create($data); // 删除记录 $condition = ['id' => 1]; $userDao->delete($condition); // 更新记录 $condition = ['id' => 2]; $data = ['name' => 'Jerry']; $userDao->update($condition, $data); // 查询单条记录 $condition = ['id' => 2]; $user = $userDao->findOne($condition); // 查询多条记录 $condition = ['age' => ['>=', 18]]; $users = $userDao->findAll($condition); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值