利用EFCore

作者: 日期:2019-08-22
 public abstract class baserepository tentity,tkey :irepository tentity,tkey where tentity : class
 private readonly dbset tentity _dbset;
 public generaldbcontext _dbcontext { get; } = null;
 /// summary 
 /// 连接字符串
 /// /summary 
 protected string _connectionstring { get; set; }
 /// summary 
 /// 数据库类型
 /// /summary 
 private databasetype _dbtype { get; set; }
 public baserepository
 _dbcontext = context;
 _dbset = _dbcontext.set tentity 
 public databasefacade database = _dbcontext.database;
 public iqueryable tentity entities = _dbset.asqueryable.asnotracking;
 public int savechanges
 return _dbcontext.savechanges;
 public async task int savechangesasync
 return await _dbcontext.savechangesasync;
 public bool any
 return _dbset.where.any;
 #region 插入数据
 public bool insert
 _dbset.add;
 if 
 return savechanges 
 return false;
 public async task bool insertasync
 _dbset.add;
 if 
 return await savechangesasync 
 return false;
 public bool insert
 _dbset.addrange;
 if 
 return savechanges 
 return false;
 public async task bool insertasync
 _dbset.addrange;
 if 
 return await savechangesasync 
 return false;
 #endregion
 #region 删除
 public bool delete
 _dbset.attach;
 _dbset.remove;
 return issavechange ? savechanges 0 : false;
 public bool delete
 entitys.foreach;
 _dbset.remove;
 return issavechange ? savechanges 0 : false;
 public virtual async task bool deleteasync
 _dbset.attach;
 _dbset.remove;
 return issavechange ? await savechangesasync 0 : false;
 public virtual async task bool deleteasync
 entitys.foreach;
 _dbset.remove;
 return issavechange ? await savechangesasync 0 : false;
 #endregion
 #region 更新数据
 public bool update
 if 
 return false;
 _dbset.attach;
 var entry = _dbcontext.entry;
 if 
 entry.state = entitystate.modified;//全字段更新
 else
 updatepropertylist.foreach.ismodified = true; //部分字段更新的写法
            
          
首页
电话
短信
联系