首页 > 代码库 > EF增删库查
EF增删库查
public async Task<bool> Add(fu_ocrresult model) { using (var db = new GENEModel()) { //1.将实体对象 a.加入 EF 对象容器中,并 b.获取 伪包装类对象 DbEntityEntry<fu_ocrresult> entry = db.Entry<fu_ocrresult>(model); //2.包装类对象的状态设置 entry.State = EntityState.Added; try { //4.提交到数据库 完成修改 var rtn = await db.SaveChangesAsync(); if (rtn > 0) { return true; } else { return false; } } catch (Exception ex) { LogHelper.Error(this, ex); return false; } } } public async Task<bool> Update(fu_ocrresult model) { using (var db = new GENEModel()) { //1.将实体对象 a.加入 EF 对象容器中,并 b.获取 伪包装类对象 DbEntityEntry<fu_ocrresult> entry = db.Entry<fu_ocrresult>(model); //2.包装类对象的状态设置 entry.State = EntityState.Modified; try { //4.提交到数据库 完成修改 var rtn = await db.SaveChangesAsync(); if (rtn > 0) { return true; } else { return false; } } catch (Exception ex) { LogHelper.Error(this, ex); return false; } } } } /// <summary> /// 获取识别明细 /// </summary> /// <param name="ocrresultid"></param> /// <returns></returns> public async Task<fu_ocrresult> GetOcrResult(string ocrresultid) { using (var db = new GENEModel()) { var query = db.fu_ocrresult.Where(x => x.OCRResultID.Equals(ocrresultid)); try { var sql = query.ToString(); LogHelper.Info(this, sql); return await query.FirstOrDefaultAsync(); } catch (Exception ex) { LogHelper.Error(this, ex); return null; } } }
//await关键字标记的异步任务,这个异步任务必须是以Task或者Task<TResult>作为返回值的 public class ValidateDal { #region 此类的实例化 private static ValidateDal fwkUersDAL = null; /// <summary> /// 此类的实例化 /// </summary> /// <returns></returns> public static ValidateDal IntiStance() { if (fwkUersDAL == null) { fwkUersDAL = new ValidateDal(); } return fwkUersDAL; } #endregion ghcConetent dbContent = new ghcConetent(); /// <summary> /// 用户登陆 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> public async Task<List<FWKUSER>> CommitValidate(string userName, string password) { var result = from c in dbContent.FWK_USER where (c.LOGIN_NAME == userName && c.PASSWORD == password) select new FWKUSER { LOGIN_NAME = c.LOGIN_NAME, USER_ID = c.USER_ID, EMPLOYEE_ID = c.EMPLOYEE_ID }; return await result.ToListAsync(); } /// <summary> /// 查询用户系统角色 /// </summary> /// <param name="userName">用户账号</param> /// <param name="sysCode">系统代码</param> /// <returns></returns> public async Task<List<FWKUSER>> GetUserRoleBySysCode(string userName,string sysCode) { //左连接 var result = from c in dbContent.FWK_USER join r in dbContent.FWK_ROLE on c.ROLE_CODE equals r.ROLE_CODE into g from cr in g.DefaultIfEmpty() join s in dbContent.FWK_SYSTEM on c.SYSTEM_ID equals s.SYSTEM_ID into g2 from crs in g2.DefaultIfEmpty() where (c.LOGIN_NAME == userName && crs.SYSTEM_ID == sysCode) select new FWKUSER { LOGIN_NAME = c.LOGIN_NAME, ROLE_CODE = c.ROLE_CODE, ROLE_NAME = cr.ROLE_NAME, SYSTEM_ID = crs.SYSTEM_ID, SYSTEM_CODE = crs.SYSTEM_CODE, SYSTEM_NAME = crs.SYSTEM_NAME }; return await result.ToListAsync(); } /// <summary> /// 根据用户名密码获取菜单Id /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns></returns> public async Task<List<fwkMenu>> GetMenuId(string userName, string password) { var result = from c in dbContent.FWK_USER join f in dbContent.FWK_USER_SYS_ROLE_REL on c.USER_ID equals f.USER_ID into g from cf in g.DefaultIfEmpty() join w in dbContent.FWK_ROLE on cf.ROLE_CODE equals w.ROLE_CODE into g2 from cfw in g2.DefaultIfEmpty() join m in dbContent.FWK_ROLE_MENU_REL on cfw.ROLE_CODE equals m.ROLE_CODE into g3 from cfwm in g3.DefaultIfEmpty() where(c.LOGIN_NAME.Equals(userName) && c.PASSWORD.Equals(password)) select new fwkMenu { Menu_Id = cfwm.MENU_ID }; return await result.ToListAsync(); } } #region 用户及科室关系操作 /// <summary> /// 更新用户及科室关系操作 /// </summary> /// <param name="model">用户及科室关系model</param> /// <returns></returns> public async Task<bool> UpdateOrg(FWK_ORG_ROLE_REL model) { Boolean result = false; using (var tran = dbConetent.Database.BeginTransaction()) { var query = from c in dbConetent.FWK_ORG_ROLE_REL where (c.USER_ID.Equals(model.USER_ID)) select c; foreach (var singerModel in query) { singerModel.ORG_ID = model.ORG_ID; singerModel.USER_ID = model.USER_ID; DbEntityEntry<FWK_ORG_ROLE_REL> entry = dbConetent.Entry<FWK_ORG_ROLE_REL>(singerModel); entry.State = EntityState.Modified; int trn = 0; try { trn = await dbConetent.SaveChangesAsync(); tran.Commit(); if (trn > 0) { result = true; } else { result = false; } } catch (Exception ex) { LogHelper.Info(this, ex); tran.Rollback(); result = false; } } return result; } } /// <summary> /// 删除用户及科室关系 /// </summary> /// <param name="model">用户及科室关系model</param> /// <returns></returns> public async Task<bool> DeleteOrg(FWK_ORG_ROLE_REL model) { Boolean result = false; var query = from c in dbConetent.FWK_ORG_ROLE_REL where (c.USER_ID.Equals(model.USER_ID) && c.ORG_ID.Equals(model.ORG_ID)) select c; using (var tran = dbConetent.Database.BeginTransaction()) { foreach (var singerModel in query) { DbEntityEntry<FWK_ORG_ROLE_REL> entry = dbConetent.Entry<FWK_ORG_ROLE_REL>(singerModel); entry.State = EntityState.Deleted; try { int rtn = await dbConetent.SaveChangesAsync(); tran.Commit(); if (rtn > 0) { result = true; } else { result = false; } } catch (Exception ex) { LogHelper.Info(this, ex); tran.Rollback(); result = false; } } return result; } } /// <summary> /// 增加用户及科室关系 /// </summary> /// <param name="model">用户及科室关系model</param> /// <returns></returns> public async Task<bool> InsertOrg(FWK_ORG_ROLE_REL model) { Boolean result = false; using (var tran = dbConetent.Database.BeginTransaction()) { FWK_ORG_ROLE_REL mode = new FWK_ORG_ROLE_REL(); mode.USER_ID = model.USER_ID; mode.ORG_ID = model.ORG_ID; mode.AUOT_ID = model.AUOT_ID; DbEntityEntry<FWK_ORG_ROLE_REL> entry = dbConetent.Entry<FWK_ORG_ROLE_REL>(mode); entry.State = EntityState.Added; try { int rtn = await dbConetent.SaveChangesAsync(); tran.Commit(); if (rtn > 0) { result = true; } else { result = false; } } catch (Exception ex) { LogHelper.Info(this, ex); tran.Rollback(); result = false; } return result; } } #endregion
EF增删库查
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。